Skip to content

Commit

Permalink
perf(ai): better rng assignment description
Browse files Browse the repository at this point in the history
  • Loading branch information
tolstenko committed Sep 12, 2023
1 parent e1b897e commit cf141e1
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions courses/artificialintelligence/assignments/rng/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ You are a game developer in charge to create a fast an reliable random number ge
- Use exactly 32 bits as seed;
- Be able to generate a number between a given range, both inclusive.

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.
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 the Marsaglia "Xorshift RNGs". You decided to implement it and test it.

## XorShift

Expand All @@ -21,13 +21,19 @@ The xorshift is a family of pseudo random number generators created by George Ma

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.

The `xorshift` algorithm from Marsaglia is a combination of 3 `xorshifts`:
!!! note

1. `xorshift` the seed by `13` bits to the left;
2. `xorshift` the seed by `17` bits to the right;
3. `xorshift` the seed by `5` bits to the left;
The value of a << b is the unique value congruent to \(a * 2^{b}\) modulo \( 2^{N} \) where \( N \) is the number of bits in the return type (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded).

At the end of this 3 `xorshifts`, the current state of the seed is your current random number.
The value of \( a >> b \) is \( a/2^{b} \) rounded down (in other words, right shift on signed a is arithmetic right shift).

The `xorshift` algorithm from Marsaglia is a combination of 3 `xorshifts`, the first one is the seed (or the last random number generated), and the next ones are the result of the previous `xorshift`. The steps are:

1. `xorshift` the value by `13` bits to the left;
2. `xorshift` the value by `17` bits to the right;
3. `xorshift` the value by `5` bits to the left;

At the end of this 3 `xorshifts`, the current state of the value is your current random number.

In order to clamp a random number the value between two numbers (max and min), you should follow this idea:

Expand Down

0 comments on commit cf141e1

Please sign in to comment.