Skip to content

Instantly share code, notes, and snippets.

View jbescoyez's full-sized avatar

Jean-Baptiste Escoyez jbescoyez

  • Brussels, Belgium
View GitHub Profile
@thejh
thejh / durstenfeld_shuffle.coffee
Created June 1, 2011 14:17 — forked from dpritchett/durstenfeld_shuffle.coffee
durstenfeld array shuffle in coffeescript
# See http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
# written as one-liners because my REPL didn't like multiline functions this morning
# tested on CoffeeScript v1.0.1
input = [0 .. 7]
swap = (input, x, y) -> [input[x], input[y]] = [input[y], input[x]]
rand = (x) -> Math.floor Math.random() * x
durst = (input) -> swap(input, i, rand i) for i in [input.length - 1 .. 1]
"""