10
Introduction
You are given a random integer generator with the following implementation
- The first invocation always returns 1.
- The second invocation returns a random integer between 1 and 2.
- The third invocation returns a random integer between 1 and 3.
- The nth invocation returns a random integer between 1 and n, inclusive.
Based on the above function, write a random dice generator that is perfectly random, returning a value between 1 and 6 (inclusive) with equal probability.
Rules
- Your program/function should result in a random integer between 1 and 6, inclusive, in some usable form, i.e., to standard output or as a function return value.
- The ascending random number generator above can be defined as a "free" function in your program (i.e., doesn't count toward your character count), or a separate script/program that is executed as needed, assuming the state (
n
) is persistent between calls. - Assume that no more than 1000 dice rolls will ever be requested in a single use case of your program, and the initial random number generator can be reset to
1
at the end of 1000 dice rolls to avoid overflow ofn
. - Your program may not use any other source of random numbers except the ascending random generator defined above. You may of course request multiple random numbers from the random number generator for each single dice roll output.
- This is code-golf, so winner is shortest answer or most votes in the event of a tie. If you can generate 1000 dice rolls using less than 1000 generated random numbers, give yourself a 10-point efficiency bonus.
Example
./asc-rand
1 # random integer between 1 and 1
./asc-rand
1 # random integer between 1 and 2
./asc-rand
3 # random integer between 1 and 3
./asc-rand
4 # random integer between 1 and 4
# dice-gen generates random dice based on output of asc-rand program.
./dice-gen
3
./dice-gen
6
./dice-gen
5
./dice-gen
1
Is the program
iterate(6):b=asc-rand(); print b
illegal or does it not work? I might be misunderstanding the third rule. – beary605 – 2012-10-01T23:52:26.593@beary605: The random number generator can only be reset after the entire 1000 dice rolls, not between every dice roll. The only reason I mention that is so dealing with possible overflows on the value returned by the random number generator is not one of the concerns in this challenge. Edit: I clarified the purpose of the rule, hope it helps. – mellamokb – 2012-10-02T14:07:54.837
When you say "random number" do you mean "random integer" or "random (truncated) real number"? Perhaps there is some convention that I am not aware of. – DavidC – 2012-10-02T14:18:59.240
@DavidCarraher: Very good point. I was meaning random integer, and I see that's not clear. I will update the question. Edit: Updated. – mellamokb – 2012-10-02T14:24:12.060
1Are we allowed to ask the randomizer how many times it has generated random numbers? I was under the impression that we could not. – Matt – 2012-10-02T16:00:52.873
@Matt: You can assume that when your program is invoked the very first time, the randomizer generates
1
. After that, you can only know if you keep track yourself of the number of times you have invoked the randomizer. – mellamokb – 2012-10-02T16:58:58.080