Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class JavaRandom {
- private static ulong Mask = (1UL << 48) - 1UL;
- private static ulong Multiplier = 0x5DEECE66DUL;
- private static ulong AddEnd = 0xBUL;
- private static ulong Seed = 0UL;
- public JavaRandom(ulong seed) {
- SetSeed(seed);
- }
- public void SetSeed(ulong seed) {
- Seed = (seed ^ Multiplier) & Mask;
- }
- public int Next(int bits) {
- ulong oldseed, nextseed;
- do {
- oldseed = Seed;
- nextseed = (oldseed * Multiplier + AddEnd) & Mask;
- if (Seed == oldseed) {
- Seed = nextseed;
- break;
- }
- } while (true);
- return (int)(nextseed >> (48 - bits));
- }
- public int NextInt(int max) {
- if ((max & -max) == max)
- return (int)((max * (long)Next(31)) >> 31);
- int bits, val;
- do {
- bits = Next(31);
- val = bits % (int)max;
- } while (bits - val + (max - 1) < 0);
- return val;
- }
- public long NextLong() {
- return ((long)Next(32) << 32) + Next(32);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment