Created
May 16, 2011 22:19
-
-
Save benfoxall/975510 to your computer and use it in GitHub Desktop.
Number Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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