Random without duplicate

I have this.

var x = Math.floor((Math.random() * 17) + 1);
hypeDocument.goToTimeInTimelineNamed(x + 5/30, ‘random’);

Is there a solution in not having the same result when the new number is generated?

I would simply store the random in a global array and check to see if the next one is in the array. If it is try again if it is not use it/ add it to array

2 Likes

The "Capitals" template has an example of this...

window.data.splice(window.question,1); // Remove the current question from the database
console.log(window.data.length + " questions remaining.");

It works like what Mark suggested. An array is created with all the questions. Once one of those questions is answered, it is removed from the array.

A new question is chosen randomly, based on the number of questions left in the array...

 window.question = Math.floor (Math.random() * window.data.length);
3 Likes

Thank you both. I’ll have a look at it.