Forum Discussion
ElizabethKra817
2 years agoCommunity Member
Random number generator avoiding duplicates. - SL3
I realize SL360 may be easier to complete this project. Alas, I cannot upgrade currently. I do, however, need help with the random number generator I am using to create a set of flash cards. One...
Jürgen_Schoene_
2 years agoCommunity Member
here is a small example, how it could work
idea
- in the storyline variable "max" you can set the number of tries (=> 50)
- create a array (list) of number: 1 .. max
- on click take a number with a random position out of the array
- window.[...] is a global storage, so different local scripts can use global js variables
on timeline start init the array
var player = GetPlayer();
var max = player.GetVar( "max" );
var numbers = [];
for( var i=1; i<=max; i++ ){
numbers.push(i);
}
window.numbers = numbers;
player.SetVar( "debug", window.numbers.toString() );
on button click
var randomIndex, ready, ret;
var player = GetPlayer();
var currLength = window.numbers.length;
if (currLength === 0){
GetPlayer().SetVar( "debug", "window.numbers.length empty" );
}
else {
ready = false;
if (currLength > 1){
randomIndex = Math.floor( Math.random() * currLength );
}
else {
randomIndex = 0;
ready = true;
}
ret = window.numbers.splice( randomIndex, 1 );
player.SetVar( "randnum", ret );
player.SetVar( "ready", ready );
player.SetVar( "debug", window.numbers.toString() );
}
https://360.articulate.com/review/content/76c46afa-6543-47f7-8c64-3d040fea47a0/review