Forum Discussion

ElizabethKra817's avatar
ElizabethKra817
Community Member
2 years ago

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 slide with 50 layers. 

I created a variable randum and used the following js to generate a random number and linked it to specific slides with triggers.  Altho tedious, that was easy.   

var randomnumber = Math.floor((Math.random()*50)+1);
var player = GetPlayer();
player.SetVar("randnum",randomnumber);

(2) Questions: 

1. How do I avoid duplicate numbers appearing regularly when I run the app?

2. How do I end the app when all cards have been used. 

Any suggestion would be helpful.  I am a js newbie. 

 

 

 

  • 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