Number set random pick then remember the extraction

Hello! I'm trying to solve this logic problem.
I have 10 rooms, I want to randomly pick a number between 1 and 10, then remember which number has been picked for the next roll (so if in the example nr. 6 is picked, next random roll is between 1-2-3-4-5-7-8-9-10, withouth the 6).

With the following script I can pick a random number from a set, knowing that 6 has been removed.
But what if I want to automate the process?

var num=[1,2,3,4,5,7,8,9,10];
var result=num[Math.floor(Math.random() * num.length)];
$gameVariables.setValue(1, result);

Once the number is picked, I want to teleport to coordinates so that nr. 5 is a definite room, and once it's picked that event will remember it's connected to nr.5.

Thank you.

Something shorter than this does exist?
Code:
function DistinctRandomNumberGenerator(min, max) {
  min = Number(min) || 0;
  max = Number(max) || 10;
  var keyTracker = {};
  var range = max - min;
 
  function getDistinctRandomNumber() {
    var rand = min + Math.floor(Math.random() * range);
    switch(!!keyTracker[rand]) {
      case true: {
        return getDistinctRandomNumber();
      }
      default: {
        keyTracker[rand] = true;
        return rand;
      }
    }
  }
 
  function getRandomKey() {
    switch (Object.keys(keyTracker).length === range + 1) {
      case true: {
        return 0;
      }
      default: {
        return getDistinctRandomNumber();
      }
    }
  }
 
  this.getRandomKey = getRandomKey;
}
 
// Usage:
var drng = new DistinctRandomNumberGenerator(1, 10);
for (var i = 0; i <= 12; i++) {
  console.log(drng.getRandomKey());
}
 
// 9 1 7 3 10 4 8 2 6 5 0 0 0
If you just want to avoid rolling the same number twice in a row, you can use two variables and some event commands, e.g.
Spoiler: event example
◆Comment:Initialise 1~10 ◆Control Variables:#0031 previous = Random 1..10 ◆Loop ◆Comment:Roll new value 1~9 (i.e. -1 range) ◆Control Variables:#0030 current = Random 1..9 ◆Comment:+1 if equal or greater than previous roll ◆If:current ≥ previous ◆Control Variables:#0030 current += 1 ◆ :End ◆Text:None, None, Window, Bottom : :\> This roll: \v[30]. : :\>(Previous roll: \v[31].) ◆Comment:Replace previous value with current one ◆Control Variables:#0031 previous = current ◆Show Choices:Roll again, End (Window, Right, #1, #2) :When Roll again ◆ :When End ◆Break Loop ◆ :End ◆ :Repeat Above
If you want to draw without replacement, you can use Array#splice to remove selected entries from the array so they aren't there next time, e.g. (source):
Spoiler: event example + script call
◆If:Script:!$gameVariables.value(1).length ◆Comment:No scenes remaining in list, or not initialised yet. ◆Control Variables:#0001 Blessings/Curses = [1,2,3,4,5] ◆ :End ◆Comment:Take a random number from the array and store in variable 2. ◆Script:var scList = $gameVariables.value(1); : :var r = Math.randomInt(scList.length); : :var scId = scList.splice(r, 1)[0]; : :$gameVariables.setValue(2, scId); ◆Text:None, Window, Bottom : :Selected scene \v[2]!
JavaScript:
var scList = $gameVariables.value(1);
var r = Math.randomInt(scList.length);
var scId = scList.splice(r, 1)[0];
$gameVariables.setValue(2, scId);
Technical details:
Thank you. How do you suggest to store coordinates and pairs for every room-room connection?
The most efficient solution depends on how you want to use the values, e.g. if there's no back-tracking then things simplify a lot: just keep a list (i.e. array) of room IDs visited.

A couple of ideas:
  1. An array of room ID pairs, e.g. [[1, 2], [2, 8]] could represent that room 1 is connected to room 2, and room 2 to room 8. You could add duplicates, e.g. [1, 2] and [2, 1], to help make lookup more efficient for back-tracking if necessary.

  2. An array that uses the index as the first room ID, e.g. [2, 8, 5] to represent room connections 1 <-> 2, 2 <-> 8, and 3 <-> 5. If there are multiple connections then each entry could be an array of connected room IDs instead.
For more, you could search for graph (topology) data structures~

Original thread: https://forums.rpgmakerweb.com/threads/number-set-random-pick-then-remember-the-extraction.149437/