|
| 1 | +# Pseudo Random Number Generation |
| 2 | + |
| 3 | +You are a game developer in charge to create a fast an reliable random number generator for a procedural content generation system. The requirements are: |
| 4 | + |
| 5 | +- Do not rely on external libraries; |
| 6 | +- Dont need to be cryptographically secure; |
| 7 | +- Be blazing fast; |
| 8 | +- Fully reproducible via automated tests if used the same seed; |
| 9 | +- Use exactly 32 bits as seed; |
| 10 | +- Be able to generate a number between a given range, both inclusive. |
| 11 | + |
| 12 | +So you remembered a strange professor talking about the xorshift algorithm and decided it is good enough for your use case. and with some small research you found a the Marsaglia "Xorshift RNGs". You decided to implement it and test it. |
| 13 | + |
| 14 | +## XorShift |
| 15 | + |
| 16 | +The xorshift is a family of pseudo random number generators created by George Marsaglia. The xorshift is a very simple algorithm that is very fast and have a good statistical quality. It is a very good choice for games and simulations. |
| 17 | + |
| 18 | +`xorshift` is the process of shifting a number and then `xor`'ing it to the original value to create a new value. |
| 19 | + |
| 20 | +```value = value xor (value shift by number)``` |
| 21 | + |
| 22 | +The shift operators can be to the left `<<` or to the right `>>`. When shifted to the left, it is the same thing as multiplying by 2 at the power of the number. When shifted to the right, it is the same thing as dividing. |
| 23 | + |
| 24 | +The `xorshift` algorithm from Marsaglia is a combination of 3 `xorshifts`: |
| 25 | + |
| 26 | +1. `xorshift` the seed by `13` bits to the left; |
| 27 | +2. `xorshift` the seed by `17` bits to the right; |
| 28 | +3. `xorshift` the seed by `5` bits to the left; |
| 29 | +4. the current state of the seed is your current random number from $0$ to $2^{32}-1$. |
| 30 | + |
| 31 | +In order to clamp a random number the value between two numbers (max and min), you should follow this idea: |
| 32 | + |
| 33 | +```value = min + (random % (max - min + 1))``` |
| 34 | + |
| 35 | +## Input |
| 36 | + |
| 37 | +Receives the seed, the number of random numbers to be generated and the range of the numbers should be in. The range is inclusive. All numbers are unsigned 32 bits integer. |
| 38 | + |
| 39 | +``` |
| 40 | +1 1 0 99 |
| 41 | +``` |
| 42 | + |
| 43 | +## Output |
| 44 | + |
| 45 | +The list of numbers to be generated, one per line. |
| 46 | + |
| 47 | +``` |
| 48 | +WiP |
| 49 | +``` |
0 commit comments