May 28, 2004
True Randomness and Code
Today Daniel has just shown me a bit of code:) With this code I was able to make something completely random. So each time you refresh the page, this story will change:) Enjoy!
pickOne = function(listOfThings){
randomNumber = Math.floor(Math.random()*listOfThings.length)
return listOfThings[randomNumber]
}
mainCharacters = new Array(
“Prince”,
“Princess”,
“Knight”,
“Knight’s Daughter”,
“Peasant Boy”,
“Stable Boy”,
“Dragon”
)
mainCharacter = pickOne(mainCharacters)
mainActions = new Array(”eat”, “have adventures”, “feed the dog”, “slay a dragon”, “program code”)
mainAction = pickOne(mainActions)
mainVillians = new Array(”a talking cat”, “a computer”, “a dragon”, “an evil knight”, “a friend”)
mainVillian = pickOne(mainVillians)
secondActions = new Array(”defeat the villain”, “eat lunch”, “retire”, “program code”, “marry royalty”, “buy a dog”)
secondAction = pickOne(secondActions)
mainItems = new Array(”sword”, “computer”, “dog”, “cat”, “best friend”, “pet dragon”)
mainItem = pickOne(mainItems)
mainDescriptions = new Array(”blue”, “hungry”, “a coward”, “useless”, “malfunctioning”)
mainDescription = pickOne(mainDescriptions)
mainEndings = new Array(”eat Pizza”, “program code”, “write stories”, “get married”, “take care of dragons”, “settle down”)
mainEnding = pickOne(mainEndings)
Once upon a time there was a document.write(mainCharacter) who wanted to document.write(mainAction) but document.write(mainVillian) prevented them. So the document.write(mainCharacter) tried to document.write(secondAction) with their document.write(mainItem) but that did not work however since their document.write(mainItem) was document.write(mainDescription).
So in the end document.write(mainCharacter) had to just document.write(mainEnding) and live happily ever after.