Skip to content

Instantly share code, notes, and snippets.

Created December 17, 2012 02:49
Show Gist options
  • Save anonymous/4315470 to your computer and use it in GitHub Desktop.
Save anonymous/4315470 to your computer and use it in GitHub Desktop.
getRandomColor: ->
number = Math.floor(Math.random() * (3 - 1 + 1)) + 1
switch number
when 1 then "#983a30"
when 2 then "#187598"
# why not
getRandomColor: -> ["#983a30", "#187598"][Math.ceil(Math.random() * 3)]
@jescalan
Copy link

getRandomColor: -> ["#983a30", "#187598"][Math.ceil(Math.random()*2)-1]

@samccone
Copy link

Math.floor(Math.random() * 2) && 0x983a30 || 0x187598

@jescalan
Copy link

getRandomItem (list) -> list[~~(Math.random()*list.length)]

... more extensible, better abstraction and design. Or if you want to get really crazy:

Array.prototype.getRandom = -> this[~~(Math.random()*this.length)]
# ["#983a30", "#187598"].getRandom()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment