Skip to content

Instantly share code, notes, and snippets.

@rohozhnikoff
Last active August 29, 2015 14:11
Show Gist options
  • Save rohozhnikoff/2d4e1b742df67c5ad7ff to your computer and use it in GitHub Desktop.
Save rohozhnikoff/2d4e1b742df67c5ad7ff to your computer and use it in GitHub Desktop.
randomize
start = 10
maxV = 100000
# ---------------------------------------------
begin = process.hrtime()
getRandom = (acc, max) ->
while acc.indexOf(random) isnt -1
random = Math.floor(Math.random() * max) + 1
random
randomize = (acc, l, max) ->
if l < 0 then acc else randomize(acc.concat([getRandom(acc, max)]), l - 1, max)
randomize([], start, maxV)
diff = process.hrtime(begin)
console.info("recursion: %ds %dms", diff[0], diff[1]/1000000)
# ---------------------------------------------
begin = process.hrtime()
val = [0..maxV]
arr = []
for i in [0..start]
j = parseInt(Math.random() * (maxV - i))
arr.push(val[j])
val.splice(j, 1)
diff = process.hrtime(begin)
console.info("for: %ds %dms", diff[0], diff[1]/1000000)
# ---------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment