Forum Discussion
Ensuring a Random Number is not Re-picked
Update: this might be the discussion thread using Javascript to remove those that have already been selected.
https://community.articulate.com/discussions/building-better-courses/trivia-game-using-variables-and-javascript-to-remove-answered-questions-from-the-deck
Still wondering about using the built in random feature though. (I can think of way to do it if I'm do a small set of random numbers, but what about between something like 1 and 1500000)
- OwenHolt7 years agoSuper Hero
I streamlined my JS for you with a slightly more elegant solution that only uses 3 variables.
WHAT YOU NEED:
- A variable to store a single text array formatted like this: 1,2,3,4,5,6,7,8,9,10,11.... through your top number. Separate values with a comma only; NOT a comma + space. In my example, the variable is named Text_Array and goes from 1 to 100.
- A variable to receive your Random Number. This can be either a text or numeric variable depending on what you want to do with it. In my example, it is a text variable named Random. My example doesn't do anything other than display the random number chosen, but obviously you would want to evaluate the value and then do something based on it like go to layer, play media, jump to slide, etc.
- A variable to track how many items are left in your list. You will use this to track when you have used all of the numbers to do something different.
- Something to trigger your JavaScript. I'm using a button. Here is the JavaScript code commented out so you can see what it is doing:
//get the StoryLine player
var player=GetPlayer();//get Storyline variable value as a string
var textArray=player.GetVar("Text_Array");//Convert string to a numeric array
numArray=textArray.split(",").map(Number);//Get a random number from the array and send it to StoryLine
var randNum = numArray[Math.floor(Math.random() * numArray.length)];
player.SetVar("Random",randNum);//Remove the random number from your array and get the array's length
numArray.splice(numArray.indexOf(randNum), 1);
var itemsLeft=numArray.length;//Convert array to a string and send it back to SL along with the array's length
textArray=numArray.map(String).toString();
player.SetVar("Items_Left", itemsLeft);
player.SetVar("Text_Array", textArray);
Example is SL360
Attached file is SL2