Skip to content

Instantly share code, notes, and snippets.

@benfoxall
Created May 16, 2011 22:19
Show Gist options
  • Save benfoxall/975510 to your computer and use it in GitHub Desktop.
Save benfoxall/975510 to your computer and use it in GitHub Desktop.
Number Generator
Number generator:
- gives a random number between 0 and N
- doesn't repeat numbers
a naive approach:
generate_1
do
i = randomNumber(0,N)
until ! get(i)
set(i, true)
return i
a better approach (?)
current = 0
generate_2
i = randomNumber(current, N)
t1 = get(current)
t2 = get(i)
# swap t1 and t2
set(i, t1)
set(current, t2) #not necessary
# increment current for the next one
current ++;
return t2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment