Working with Randomization
So lately we’ve been working on a project to create a game of Jeopardy. I could talk for days about all of the problems I encountered, all of the fun tips and tricks I picked up along the way, as well as my fun times with Angular.
But I won’t do that.
Instead I’ll talk about a very particular problem that actually interested me quite a bit, which was this:
“Create 3 random daily doubles”
For simplicity’s sake, let’s assume that we’re already further ahead in the project and our board looks something like this:
Also for simplicity’s sake, let’s assume that our data currently exists in a JavaScript array with five categories, each containing an array with five clues, of which are objects. (I know the picture has four, but whatever!)
With that out of the way, let’s break this down a bit!
Breaking it down
The problem, again, is to “create 3 random daily doubles”. From this we know two things:
1: We need to get our daily doubles randomly, as in they cannot be reliably predicted.
2: We need to get exactly 3 of them. No more, no less.
That’s it! So now we need to get started.
Because we’re working with objects, and objects are pass by reference, we don’t need to worry too much. We’ll settle for adding a “dailyDouble” property to them. This will be a Boolean. If it is a daily double it will have this property set to true.
So, onto the interesting bit, the randomization!
This shouldn’t be bad, right? Let’s start by just throwing every single object item into an array, like so:
let allClues = [];
for( let i = 0; i < data.length; i++) {
for(let j = 0; j < data[i].length; j++) {
allClues.push(data[i][j];
}
}This will place every object into the array, making it easier for us to manipulate. For the randomization, this will take a bit of thinking. We *could* just create three random variables, and then grab those items:
allClues[Math.Floor(Math.random() * allClues.length)].dailyDouble = true;and then do that a total of three times:
for(let I = 0; i < 3; i++) {
allClues[Math.Floor(Math.random() * allClues.length)].dailyDouble = true;
}But then we run into an issue. How do we avoid duplicates?
Get Christopher Tiller’s stories in your inbox
Join Medium for free to get updates from this writer.
Well, we could fix that by placing it in an array, and then checking if a value has shown up yet…
let temp = [];
for(let i = 0; i < 3; i++) {
let randVal = Math.Floor(Math.random() * allClues.length);
if(temp.indexOf(randVal) !== -1) {
allClues[randVal].dailyDouble = true;
}
}And this would work! We no longer get duplicates, but we can still get less than three daily doubles. We *must* get three!
So, let us address this:
let temp = [];
while(temp.length < 3) {
let randVal = Math.Floor(Math.random() * allClues.length);
if(temp.indexOf(randVal) !== -1) {
allClues[randVal].dailyDouble = true;
}And this will work! That is, unless, we get really unlucky and keep getting the same value over and over again. While rare, there is a nonzero probability it can happen, and we want to avoid that.
So, what do we do then?
Well, the problem is we want three random items, so why not just randomize the entire array instead? The array in particular isn’t sacred or important beyond being a place to actually hold our data, so we can do with it what we please.
To do this we can use what is called the durstenfield shuffle. It loops backwards and randomizes the elements.
function shuffleArray(array) {
for (var i = array.length — 1; i > 0; i — ) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}// Taken from stackOverflow
So how does this work?
Use a shuffle algorithm to randomize the array, and then just take the first three values of that list and set the dailydouble value on each of them to true.
let dailyDoubles = [];
let temp;
for(let I = 0; I < allClues.length — 1; i — ) {
let j = Math.floor(Math.random() * (i + 1));
let temp = allClues[i];
allClues[i] = allClues[j];
allClues[j] = temp;
}And then:
for(let I = 0; I < 3; i++) {
allClues[i].dailyDouble = true;
}And voila! We’re done! We have 3 daily double properties on three random clues.
To recap, the idea was:
Instead of trying to create random values and use those, instead just randomize your list of values, and pick x amount from that list.
That’s it! Let me know what you think in the comments!




