Welcome to Game of Random Thrones!

GoT is back! In the honor of Winter coming soon, this post demonstrates how to use JavaScript to randomize your data. Business case: let’s say you have a list of character names from Game of Thrones, as well as the actor/actress who plays them. Nicely ordered, so the two lists match. You want to randomize the list of characters and display them in Storyline.

[thrive_link color=’blue’ link=’https://www.rabbitoreg.com/examples/random/story.html’ target=’_blank’ size=’medium’ align=”]TRY IT[/thrive_link]

Note: this is not the same as picking one character randomly. It is to randomly shuffle the whole list. However, you’ll see that it is actually done by randomly picking one character multiple times.

First thing come to you mind is an array. An array basically a list of stuff you keep together. Like character names.

var gotCharacters = [“Arya Stark”,”Bran Stark”,”Brienne of Tarth”,”Bronn”,”Catelyn Stark”,”Cersei Lannister”,”Daario Naharis”,”Daenerys Targaryen”,”Davos Seaworth”,”Ellaria Sand”,”Gendry”,”Gilly”,”Jaime Lannister”,”Jaqen H’ghar”,”Jeor Mormont”,”Joffrey Baratheon”,”Jon Snow”,”Jorah Mormont”,”Khal Drogo”,”Margaery Tyrell”,”Melisandre”,”Missandei”,”Ned Stark”,”Petyr Baelish”,”Ramsay Bolton”,”Robb Stark”,”Robert Baratheon”,”Roose Bolton”,”Samwell Tarly”,”Sandor Clegane”,”Sansa Stark”,”Shae”,”Stannis Baratheon”,”Talisa Stark”,”The High Sparrow”,”Theon Greyjoy”,”Tommen Baratheon”,”Tormund Giantsbane”,”Tyrion Lannister”,”Tywin Lannister”,”Varys”,”Viserys Targaryen”,”Ygritte”];

Now we have an array, named gotCharacters, and all the fave dead or alive creatures. Another array can hold the actual names of actors and actresses:

var gotNames = [“Maisie Williams”,”Isaac Hempstead Wright”,”Gwendoline Christie”,”Jerome Flynn”,”Michelle Fairley”,”Lena Headey”,”Michiel Huisman”,”Emilia Clarke”,”Liam Cunningham”,”Indira Varma”,”Joe Dempsie”,”Hannah Murray”,”Nikolaj Coster-Waldau”,”Tom Wlaschiha”,”James Cosmo”,”Jack Gleeson”,”Kit Harington”,”Iain Glen”,”Jason Momoa”,”Natalie Dormer”,”Carice van Houten”,”Nathalie Emmanuel”,”Sean Bean”,”Aidan Gillen”,”Iwan Rheon”,”Richard Madden”,”Mark Addy”,”Michael McElhatton”,”John Bradley-West”,”Rory McCann”,”Sophie Turner”,”Sibel Kekilli”,”Stephen Dillane”,”Oona Chaplin”,”Jonathan Pryce”,”Alfie Allen”,”Dean-Charles Chapman”,”Kristofer Hivju”,”Peter Dinklage”,”Charles Dance”,”Conleth Hill”,”Harry Lloyd”,”Rose Leslie”];

So far so good. You can always refer to one of these elements with the name of the array and the position:

gotNames[2] would be Gwendoline Christie (remember geeks start counting from 0)

The rest of the “programming” should randomize the arrays:

function shuffle()

{

// to do: shuffle gotCharacters array

}

Here’s a problem: if you randomly shuffle the gotCharacters array, and do nothing with gotNames, you’ll end up with a mismatch between the order of characters and actors. Believe me, you don’t want Gwendoline Christie to play the The High Sparrow. So, we’re going to shuffle gotCharacters randomly BUT if we switch any of the characters in the gotCharacters array, we’re going to switch the same two elements in gotNames as well. This way, the two arrays keep the character and real name order the same.

The shuffling here is simply going through every element of the gotCharacters array, and randomly select a new position for the character. Once we have a randomly selected position, we swap the elements within the array. (For example, you swap gotCharacter[3] and gotCharacter[10].) This is not the most scientific randomization but gives you enough gunpowder to shake up the order every time you run it.

The shuffling code is in the zsolt_got.js file in the /source folder with the examples. You can download it here.

[thrive_link color=’blue’ link=’https://www.rabbitoreg.com/examples/random/source/got.zip’ target=’_blank’ size=’medium’ align=”]Download SL 360 and JS[/thrive_link]

This example simply places the randomized names in a Storyline text variable to display. You can also have multiple variables like char1, char2, char3, etc. In that case, you could set these individual SL variables one by one once the arrays are shuffled.

(Pro Tip: you don’t need to write 40 lines of code like var player = GetPlayer(); p.SetVar(“char1”,gotCharacter[0]); p.SetVar(“char2”,gotCharacter[1]); p.SetVar(“char3”,gotCharacter[2]); etc. You can put them into a loop and generalize it. Here’s an example.)