MV [Solved] Javascript Array for Random Number Generator Without Repeats for Variables

Hello Everyone, how are you doing? I'm developing a game for my birthday, where it will be used a as game show to quiz people on video game trivia and video game challenges. Most of the fundamental pieces of codes are working, however one is incomplete, and that's the random number generator (without repeats) for the categories that will be pick for the ten questions/challenges that the contestant will go through. I've told one of my friends IRL about this problem, and he said that what I would need is a hash table, he even showed me how to code it, but the code is in C# instead of javascript, which he has no experience with. As I tried to lookup forms on here, and even videos to find a solution to my problem, one of them came close to what I was looking for, but it was missing how to reset the array, cause in my game, once you move to a new map when the contestant gets a question wrong, I want an event so the random number generator will pick a new set of categories from the same array. Here's the form post for reference: Random number variable without repeat

I'm asking for help since I know little to nothing about javascript, and the post linked above is for RPG Maker VXAce, not in javascript so I may use it in RPG Maker MV. Any and all help is appreciated, and if you can, please provide some code so I may copy paste it into my game. Thanks in advance, and I'll be sure to give you credit in my game as a token of my gratitude.

Edit: Sorry guys, I should have mentioned that the code I was referring to was by Shaz in the link above, since the link by wavelength no longer works.
Do you have the original code from that post? Wavelength's link no longer works.
Do you have the original code from that post? Wavelength's link no longer works.
The code I was referring to was by Shaz, you should see it if you scroll down.
The equivalent code would be:

Ruby:
Script: $game_variables[1] = []100.times do i  $game_variables[1].push(i)end

JavaScript:
$gameVariables.setValue(1, Array.from(Array(100).keys()));

To get a number:

Ruby:
Control Variables [0002: Random Number] = Script: $game_variables[1].delete_at(rand($game_variables[1].size))

JavaScript:
const arr = $gameVariables.value(1); const rand = arr[Math.randomInt(arr.length)]; arr.remove(rand); rand

To add a number back (3, for example):

Ruby:
Script: $game_variables.push(3)

JavaScript:
$gameVariables.value(1).push(3);
The equivalent code would be:

Ruby:
Script: $game_variables[1] = []100.times do i  $game_variables[1].push(i)end

JavaScript:
$gameVariables.setValue(1, Array.from(Array(100).keys()));

To get a number:

Ruby:
Control Variables [0002: Random Number] = Script: $game_variables[1].delete_at(rand($game_variables[1].size))

JavaScript:
const arr = $gameVariables.value(1); const rand = arr[Math.randomInt(arr.length)]; arr.remove(rand); rand

To add a number back (3, for example):

Ruby:
Script: $game_variables.push(3)

JavaScript:
$gameVariables.value(1).push(3);
Thanks so much! I’ll be sure to put you in my credits for my game.
The equivalent code would be:

Ruby:
Script: $game_variables[1] = []100.times do i  $game_variables[1].push(i)end

JavaScript:
$gameVariables.setValue(1, Array.from(Array(100).keys()));

To get a number:

Ruby:
Control Variables [0002: Random Number] = Script: $game_variables[1].delete_at(rand($game_variables[1].size))

JavaScript:
const arr = $gameVariables.value(1); const rand = arr[Math.randomInt(arr.length)]; arr.remove(rand); rand

To add a number back (3, for example):

Ruby:
Script: $game_variables.push(3)

JavaScript:
$gameVariables.value(1).push(3);
I tried using the get number script you provided, and the game reports back saying "arr.remove is not a function". I think what you meant was arr.splice if my research is correct, but when I try to use that it deletes my event. I also would like to know, how do I store the value into another variable(s) so I can push all the values it pulls in one script. Thanks in advance.
I tried using the get number script you provided, and the game reports back saying "arr.remove is not a function". I think what you meant was arr.splice if my research is correct. I also would like to know, how do I store the value into another variable(s) so I can push all the values it pulls in one script. Thanks in advance.
Oh, I didn't realise you were using MV, remove is an array function added in MZ's core. .splice will give the same result, but you'll have to use indexOf or findIndex to get the index the random value was at.

To store the value into another variable, you can insert a $gameVariables.setValue(2, rand); in between the const rand declaration and the final return value.
Oh, I didn't realise you were using MV, remove is an array function added in MZ's core. .splice will give the same result, but you'll have to use indexOf or findIndex to get the index the random value was at.

To store the value into another variable, you can insert a $gameVariables.setValue(2, rand); in between the const rand declaration and the final return value.
I tried using splice, but it deletes my event, I don't know if my syntax is wrong or what.
JavaScript:
const arr = $gameVariables.value(22); const rand = arr[Math.randomInt(arr.length)]; $gameVariables.setValue(2, rand); arr.splice(rand, 1); rand
Game variable 22 is where I'm using the array, and game variable 2 is where the random number to determine the category for the 1st question/challenge will be stored.
I tried using splice, but it deletes my event, I don't know if my syntax is wrong or what.
JavaScript:
const arr = $gameVariables.value(22); const rand = arr[Math.randomInt(arr.length)]; $gameVariables.setValue(2, rand); arr.splice(rand, 1); rand
Game variable 22 is where I'm using the array, and game variable 2 is where the random number to determine the category for the 1st question/challenge will be stored.
Like I said, you need to use indexOf because the first argument to splice is the index to start at, not the element to remove. It's taking your random number as the starting index.
Like I said, you need to use indexOf because the first argument to splice is the index to start at, not the element to remove. It's taking your random number as the starting index.
Forgive me, I’m not accustom to JavaScript like you are, all I know is how to code in C++, a little bit of Java, a little bit of C#, HTML5, and a little bit of CSS. I’ll resume working on the game when I have time, I’ll update you here should something come up. Thanks for all your help.
Forgive me, I’m not accustom to JavaScript like you are, all I know is how to code in C++, a little bit of Java, a little bit of C#, HTML5, and a little bit of CSS. I’ll resume working on the game when I have time, I’ll update you here should something come up. Thanks for all your help.
JavaScript:
const arr = $gameVariables.value(1); const rand = arr[Math.randomInt(arr.length)]; $gameVariables.setValue(2, rand); arr.splice(arr.indexOf(rand), 1); rand
Another approach, in case it helps:
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:
JavaScript:
const arr = $gameVariables.value(1); const rand = arr[Math.randomInt(arr.length)]; $gameVariables.setValue(2, rand); arr.splice(arr.indexOf(rand), 1); rand
Thanks, I’ll test it out as soon I can, trust me your help won’t go unnoticed. Again, thanks so much for all your help, and you have a nice one!
Another approach, in case it helps:
Thanks, I’ve seen that function while I was doing some research, and now I’ll take a look at it when I have the chance. You have a nice one!

Edit: I just notice the script you left for me, I’ll test it when I get a chance.
JavaScript:
const arr = $gameVariables.value(1); const rand = arr[Math.randomInt(arr.length)]; $gameVariables.setValue(2, rand); arr.splice(arr.indexOf(rand), 1); rand
Just tested it and it works like a charm, thanks so much for your help! Sorry if I gave you a hard time, like I said, I know little to nothing about JavaScript. I'll be sure to add your username to the credits for sure.
Just tested it and it works like a charm, thanks so much for your help! Sorry if I gave you a hard time, like I said, I know little to nothing about JavaScript. I'll be sure to add your username to the credits for sure.
I don't consider you to have given me a hard time at all. XD Glad it worked for you!

@Omarproductions, please avoid double posting, as it is against the forum rules. You can use the "Edit" function on your posts to add additional information you've forgotten or respond to multiple people. You can review our forum rules here. Thank you.

Good to hear you got it working, though! 😂

If the problem is solved, consider reporting your post with a reason like "solved". That'll highlight it for the mods, who can close the thread.
Double-post notice: @Omarproductions Good to hear you got it working, though! 😂

If the problem is solved, consider reporting your post with a reason like "solved". That'll highlight it for the mods, who can close the thread.
Thanks for the warning, I didn’t even know I double posted, I’ve gone ahead an edited the title with “[Solved]” at the beginning. Thanks to everyone for your help!

Edit: I’ve just reported one of the posts at solved, again thanks for everyone’s help!

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/solved-javascript-array-for-random-number-generator-without-repeats-for-variables.152479/