"Drawing Items Out of a Hat"-esque RNG?

Background:


Note: While I have a specific use in mind for what I am inquiring how to do, I've tried to keep the actual question portion as general as possible because I think what I'm asking about might have applications in more usage cases than just the thing I'm trying to do. I feel like I have an idea on how conceptually to do it, but I don't know how to use script calls to actually make it happen. My only real programming experience is basic Python.


I'm attempting to create a system to semi-randomly generate a dungeon, with the rooms being predefined but their arrangement being random, as a sort of gimmick for one specific dungeon I have in mind. (Note - even if there's a way to fully randomly generate a dungeon, I intend on only randomizing the map connections so that I have more control over the dungeon's aesthetics than a completely randomly generated dungeon would allow). Currently I'm in a proof-of-concept phase with it, making an experimental project as a feasibility study.


The current implementation I've come up with involves generating a random number, using switches to check if the room is in use or not, and then if the room has already been used, try again. The problem is, though, as the number of rooms increases this gets a bit inefficient, because the probability of selecting an unused room gets lower over time. I'd like to know if there's a way that I could make the random number generator work more like drawing items out of a hat (and not replacing them). I assume this would be beyond the capabilities of the event editor's built in commands but I'm hoping it may be possible with script calls. It is okay if the list of possible random numbers to generate has to be created by hand - this might even be preferable because then I can use Map ID's directly instead of having events intended to convert the RNG result into an actual, usable Map ID.


The most important part of the question is the RNG implementation itself, as it should be applicable to more scenarios than the one I'm using (that said, if you know a better way to do what I'm trying to do, by all means tell me). As an example of what I want to happen in a very general form:


RNG chooses from the following list: [1, 2, 3, 4]


Run some events based on this choice.


Suppose "3" was chosen.
RNG chooses another value from the list, which is now [1, 2, 4]


Run some more events based on this choice.


The list needs to be restored to its original state whenever the common event used to generate the dungeon is called.


It is also preferable if I can check how many items are on the list (this will be especially important when I look into implementing a system to make sure everything connects properly, which I have a rough idea of how to go about doing that needs a proof-of-concept still).


I have a thought on the implementation, but I don't know how to make use of Javascript (I assume script calls would use that as well, yes?) to pull it off:


Create a list. A preset one, hard-coded into the common event governing the dungeon generation, should suffice.


Use a random number between 1 and n, where n is the size of the list, to select an element from the list. (may actually be 0 and/or n-1 because indexing; I'm not sure how it works in Javascript, assuming Javascript even HAS lists in it like Python does)


Store this number in a variable and remove it from the list.
Yes, and it's really simple, and you can do it with events and a couple of script calls.


At the start of the game, put the list of map numbers available to choose from into an array.  Now create a second array (I'd use a variable for this, as it will be saved with the player's data) to build a list of rooms to visit, in order, by pulling numbers from the first array randomly, then deleting them from the array so they can't be chosen again.  Your variable (second array) will now have all the rooms that will be visited, in the order in which they will be visited.  It's up to you whether you want to use all the rooms or just some of them.


Allocate a second variable to keep track of the current room number, and set its value to -1.


Now create a common event for 'next room' and a common event for 'previous room'.  The 'next room' common event will add 1 to the 'current room' variable, then do a series of conditional branches to determine what map that is, and will transfer the player to the appropriate coordinates on that map.  The 'previous room' common event will subtract 1 from the 'current room' variable, then do a series of conditional branches to determine what map that is, and will transfer the player to the appropriate coordinates on that map.  If the 'current room' variable is -1, the player will go back to the prior-to-dungeon-entry map.  If it's equal to the array length, they will go to the other-side-of-dungeon map.
Thanks! This gave me enough information to make a good search on how to use Javascript to do what I needed to do (for the arrays - unless MV has a built in way of handling them that I've somehow never seen).


I'll need a slightly more complicated system for the map connections than what you propose, but I have a few ideas on how to make that work so I'll test those before asking for further assistance. It's probably not a bad idea to at least briefly use that system for the proof-of-concept testing, though, as it may give me some insight into how to mold it into something that can handle a more complex layout.


Here's the script call I used to test the sorting. Forgive the strange variable names; that's just something I tend to do while testing things. Probably not good practice but whatever. I haven't messed with accessing the game's list of variables yet. I don't know if they'd handle anything other than a number, but I figure the worst case scenario if I test it is that it crashes, which is fine when testing stuff.

Code:
var test = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
var cat = [];
while (cat.length < 15) {
    var temp = Math.floor(Math.random() * test.length);
    cat.push(test[temp]);
    test.splice(temp, 1);
}
$gameMessage.add(" " + cat);
Yeah, that's the sort of thing I was alluding to.  I hadn't read your whole post when I made that reply - I see you might want to rebuild the list - re-randomize during the game?  If so, instead of putting it in an event at the start of the game, put it all into a common event, and just call the common event whenever you want to regenerate the sequence.

Original thread: https://forums.rpgmakerweb.com/threads/drawing-items-out-of-a-hat-esque-rng.71143/