Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
FastRandom.cs
Go to the documentation of this file.
1using System;
2
3namespace Terraria.Utilities;
4
5public struct FastRandom
6{
7 private const ulong RANDOM_MULTIPLIER = 25214903917uL;
8
9 private const ulong RANDOM_ADD = 11uL;
10
11 private const ulong RANDOM_MASK = 281474976710655uL;
12
13 public ulong Seed { get; private set; }
14
15 public FastRandom(ulong seed)
16 {
17 this = default(FastRandom);
18 Seed = seed;
19 }
20
21 public FastRandom(int seed)
22 {
23 this = default(FastRandom);
24 Seed = (ulong)seed;
25 }
26
27 public FastRandom WithModifier(ulong modifier)
28 {
29 return new FastRandom(NextSeed(modifier) ^ Seed);
30 }
31
32 public FastRandom WithModifier(int x, int y)
33 {
34 return WithModifier((ulong)(x + 2654435769u + ((long)y << 6)) + ((ulong)y >> 2));
35 }
36
38 {
39 return new FastRandom((ulong)Guid.NewGuid().GetHashCode());
40 }
41
42 public void NextSeed()
43 {
45 }
46
47 private int NextBits(int bits)
48 {
50 return (int)(Seed >> 48 - bits);
51 }
52
53 public float NextFloat()
54 {
55 return (float)NextBits(24) * 5.9604645E-08f;
56 }
57
58 public double NextDouble()
59 {
60 return (float)NextBits(32) * 4.656613E-10f;
61 }
62
63 public int Next(int max)
64 {
65 if ((max & -max) == max)
66 {
67 return (int)((long)max * (long)NextBits(31) >> 31);
68 }
69 int num;
70 int num2;
71 do
72 {
73 num = NextBits(31);
74 num2 = num % max;
75 }
76 while (num - num2 + (max - 1) < 0);
77 return num2;
78 }
79
80 public int Next(int min, int max)
81 {
82 return Next(max - min) + min;
83 }
84
85 private static ulong NextSeed(ulong seed)
86 {
87 return (seed * 25214903917L + 11) & 0xFFFFFFFFFFFFuL;
88 }
89}
static Guid NewGuid()
Definition Guid.cs:1283
static FastRandom CreateWithRandomSeed()
Definition FastRandom.cs:37
static ulong NextSeed(ulong seed)
Definition FastRandom.cs:85
FastRandom WithModifier(int x, int y)
Definition FastRandom.cs:32
FastRandom WithModifier(ulong modifier)
Definition FastRandom.cs:27
int Next(int min, int max)
Definition FastRandom.cs:80