Need to assign a set of non repeating numbers to variables.

Hi there.

i am trying to get a randomized npc system going. What I did was to use a bunch of actors and used some scripting magic to make events use their graphics. I created a bunch of variables to pick random numbers corresponding to the actors and then use these variables to control the individual events.

This is easy to set up and easily expandable but, there is a problem... duplicates.
The Eventing system does not allow for lists etc. to be used. So I need something that ensures i do not end up with duplicates. It would be possible using conditional branches, looping back in to themselves. But with a larger number of options for npcs this is just not practical.

Long story short, I need certain variables (in my test build 41-50) to be given a value from a range of possible numbers ( in my test build this would be 200-210) without any number being assigned twice.

Thanks in advance for any help.

P.S. Dear mods, i was unsure if this should go in to the general support or the JS support forums. Please move the post if need be.
Code:
for (var i = 41; i < 51; i++) {
var tempArr = [];
var x = Math.randomInt (185235821);
while (tempArr.includes(x)) x = Math.randomInt (185235821);
tempArr.push (x);
$gameVariables.setValue (i, x);
}
Insert this to a script command (3rd page) in your event page. Just be sure to set your numbers to whatever you need to. Btw. These random numbers will not be limited by other minimum than zero. In case you need a custom minimum:
Code:
for (var i = 41; i < 51; i++) {
var tempArr = [];
var x = Math.randomInt (185235821);
while (x <= 150000) x = Math.randomInt (185235821);
while (tempArr.includes(x)) x = Math.randomInt (185235821);
tempArr.push (x);
$gameVariables.setValue (i, x);
}
Code:
for (var i = 41; i < 51; i++) {
var tempArr = [];
var x = Math.randomInt (185235821);
while (tempArr.includes(x)) x = Math.randomInt (185235821);
tempArr.push (x);
$gameVariables.setValue (i, x);
}
Insert this to a script command (3rd page) in your event page. Just be sure to set your numbers to whatever you need to. Btw. These random numbers will not be limited by other minimum than zero. In case you need a custom minimum:
Code:
for (var i = 41; i < 51; i++) {
var tempArr = [];
var x = Math.randomInt (185235821);
while (x <= 150000) x = Math.randomInt (185235821);
while (tempArr.includes(x)) x = Math.randomInt (185235821);
tempArr.push (x);
$gameVariables.setValue (i, x);
}

Thanks for the reply. But I get a "undefined is not a function" error.
Also what exactly is a custom minimum?
I forgot versions sub 1.6 use older javascript version 😀
Instead of includes try contains in the script.
And the custom minimum is simple, sometimes you need a random number from let's say 30 to 50.
I forgot versions sub 1.6 use older javascript version 😀
Instead of includes try contains in the script.
And the custom minimum is simple, sometimes you need a random number from let's say 30 to 50.

Alright I got it to run, but I still do get duplicates.

This is what i ended up using, what am i doing wrong?

Code:
for (var i = 41; i < 51; i++) {
var tempArr = [];
var x = Math.randomInt (210);
while (x <= 200) x = Math.randomInt (210);
while (tempArr.contains(x)) x = Math.randomInt (210);
tempArr.push (x);
$gameVariables.setValue (i, x);
}
Sorry, I made a mistake in the code.
Code:
var tempArr = [];
for (var i = 41; i < 51; i++) {
var x = Math.randomInt (210);
while (x <= 200) x = Math.randomInt (210);
while (tempArr.contains(x)) x = Math.randomInt (210);
tempArr.push (x);
$gameVariables.setValue (i, x);
}
For some reason this code now doesn't generate duplicates, but generates numbers under 200 even though it shouldn't. The reason for that is, I suspect, maximum call stack size. In other words the max amount a line of code is willing to call itself.
Interesting.... is there any way around this?
Code:
var tempArr = [];
for (var i = 0; i < 10; i++) tempArr.push (201 + i);
for (var i = 0; i < tempArr.length; i++) {
var temp = Math.randomInt (tempArr.length -1);
$gameVariables.setValue (i + 41, tempArr[temp]);
tempArr.splice (temp, 1);
}
There is no better method. If you need to input some values manually, input them like this
var tempArr = [3, 24, 333, 3028, 2343];
Ok so this only fills the first 5 variables? Is that Intentional? (Thanks for your continued patience btw)
That is not intentional. That is weird and completely unnatural and looks like a bug in the program!

Edit: Caught the bug.

Code:
var tempArr = [];
for (var i = 0; i < 10; i++) tempArr.push (201 + i);
var x = tempArr.length;
for (var i = 0; i < x; i++) {
var temp = Math.randomInt (tempArr.length -1);
$gameVariables.setValue (i + 41, tempArr[temp]);
tempArr.splice (temp, 1);
}
That is not intentional. That is weird and completely unnatural and looks like a bug in the program!

Edit: Caught the bug.

Code:
var tempArr = [];
for (var i = 0; i < 10; i++) tempArr.push (201 + i);
var x = tempArr.length;
for (var i = 0; i < x; i++) {
var temp = Math.randomInt (tempArr.length -1);
$gameVariables.setValue (i + 41, tempArr[temp]);
tempArr.splice (temp, 1);
}


YES! You are my hero! Thank you so much.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/need-to-assign-a-set-of-non-repeating-numbers-to-variables.89307/