Skip to content

Instantly share code, notes, and snippets.

@joshhunt
Created July 3, 2015 12:51
Show Gist options
  • Select an option

  • Save joshhunt/0b4e28a518643c1a7608 to your computer and use it in GitHub Desktop.

Select an option

Save joshhunt/0b4e28a518643c1a7608 to your computer and use it in GitHub Desktop.
randomChoice = (items) ->
item = items[Math.floor(Math.random()*items.length)]
shuffle = (array) ->
currentIndex = array.length
temporaryValue = randomIndex = null
# While there remain elements to shuffle...
while 0 isnt currentIndex
# Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex -= 1
# And swap it with the current element.
temporaryValue = array[currentIndex]
array[currentIndex] = array[randomIndex]
array[randomIndex] = temporaryValue
array
validLetters = ['X', 'C', 'K']
validNumbers = ['3', '4', '7']
makeCode = ->
code = [
randomChoice validLetters
randomChoice validLetters
randomChoice validNumbers
randomChoice validNumbers
randomChoice validNumbers
randomChoice validNumbers
randomChoice validNumbers
randomChoice validNumbers
]
shuffle(code).join('')
# Now actually make a bunch
for i in [0...20]
console.log makeCode()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment