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?
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