# Rolling die and random numbers

Hello (and best wishes for the coming year)

Using examples I've found on this thread, I'm building a challenge with a "roll the die" button to give "randomly access" to questions. I've found a lot of answers to the difficulties I encountered on my way. Thanks to all the contributors !

I'm using Storyline 3 and I've used a short javascript to create random numbers from 2 to 11 as I've got 10 questions. I've got a layer "roll the dice" and a "result of the dice" in the same way I've seen here before.

It seems to work fine, but at some point, the "game" freezes. Sometimes I can roll the dice 3 times, but it can be more ... or less ! I can't find what is going wrong and I was wondering if someone here has already encouter such a difficulty.

Regards

###### 11 Replies Hi Owen

1) I don't allow repeated results : once the number has been showed once, it can't come back again because it leads to a question and each question can be answered only once.

2) here is the javascript code

var nombreAlea=Math.floor(Math.random()*9+2);
p=GetPlayer();
p.SetVar("deEnCours",nombreAlea);

3) that's not easy, it's not anonymised for the moment... I'll do it before the end of the week.

thanks again. I've put the rolling die aside for the moment, the questions are shown without any "game" before. But I'm interested in any solution to be able to add more fun to this quiz. Nice example Josh, love the roll animation.  Can I borrow (steal) it?
The piece missing from your example is that Marine doesn't want to repeat rolls.  Each outcome from 2 dice being rolled must equal a unique number between 2 (snake eyes) and 11 (because she only wants 10 outcomes).  I'm not sure the built-in random generator can handle that as efficiently as JS without a LOT of triggers and additional variables. Ah I missed the part about having unique numbers. Yeah much easier in JavaScript.

I can't share the source file because it's client work but I've attached a screenshot of the motion path.

I used these setting for the motion path - Duration: 0.9, Direction: none, Speed: slow

And these setting for the entrance animation - Spin, Duration:0.9, Direction: counterclockwise, Amount: two spins. I took a purely JavaScript approach and created a file using a couple of your course assets. You can see it in action here: Link to Published SL360 File

You need a variable to store the possible roll totals as a single text string: 2,3,4,5,6,7,8,9,10,11. You also need variables for the value of dice 1, the value of dice 2, and one for the total of the 2 dice.  You might also want one to track how many questions have been answered so you know when you are done.

The JavaScript is a bit more complex than what you are using because you need to pull and modify the array (to avoid duplicates) and you need to create a dynamic array for one of the dice.
The reason for this is that not all values are available at all times.  For example, if your total is supposed to be 5, dice one can only be a 1, 2, 3, or 4. Dice 2 will be the remaining value.  If, on the other hand, your total is supposed to equal 9, your dice one values cannot be 1 or 2 as the highest value of the 2nd dice of 6 would not be enough to total 9.  Once you have the dynamic array for the die with the correct possible values, you can select a random member and with a little math, determine the required value for the second dice.

The Java Script (with notes) looks like this:

//get the StoryLine player
var player=GetPlayer();

//get Storyline variable value as a string
var diceTotal=player.GetVar("Dice_Total");

//Convert string to a numeric array
diceTotal=diceTotal.split(",").map(Number);

//Get a random dice total from that array and remove it
var randNum = diceTotal[Math.floor(Math.random() * diceTotal.length)];
diceTotal.splice(diceTotal.indexOf(randNum), 1);

//Create an empty array for the value of the 1st dice and fill it with valid values
var d1=[];
if(randNum<8) {
for (var i = 1; i <= randNum-1; i++) {
d1.push(i);}
} else {
for (var i = 6; randNum-i <= 6; i--) {
d1.push(i);}
};

//Pull a random value from the available array for dice 1
var diceOne = d1[Math.floor(Math.random() * d1.length)];

//Subtract the dice one value from the dice total to get the second dice value
var diceTwo = randNum - diceOne;

//Convert the diceTotal array back to a string
diceTotal=diceTotal.map(String).toString();

//Send the values back to SL
player.SetVar("Dice_Total",diceTotal);
player.SetVar("TotalOfDice",randNum);
player.SetVar("Dice_One",diceOne);
player.SetVar("Dice_Two",diceTwo);

SL3 file attached.