44namespace Gameframe . Procgen
55{
66 /// <summary>
7- /// Noise based random number generator
7+ /// Noise based random number generator (Struct version)
88 /// Allows for fast random-access within a sequence of generated numbers
99 /// Based on GDC 2017 talk and code by Squirrel Eiserloh
1010 /// </summary>
11- public class NoiseRng : IRandomAccessRandomNumberGenerator
11+ public struct RandomGeneratorStruct : IRandomAccessRandomNumberGenerator
1212 {
1313 private uint seed ;
1414 private int position ;
@@ -17,21 +17,12 @@ public class NoiseRng : IRandomAccessRandomNumberGenerator
1717 /// Initialize random number generator with a given seed
1818 /// </summary>
1919 /// <param name="seed">see for the random number generator</param>
20- public NoiseRng ( uint seed )
20+ public RandomGeneratorStruct ( uint seed )
2121 {
2222 this . seed = seed ;
2323 position = 0 ;
2424 }
2525
26- /// <summary>
27- /// Seeds using DateTime.UtcNow.Ticks
28- /// </summary>
29- public NoiseRng ( )
30- {
31- this . seed = ( uint ) DateTime . UtcNow . Ticks ;
32- position = 0 ;
33- }
34-
3526 /// <summary>
3627 /// Current position in the number generator sequence
3728 /// </summary>
@@ -194,4 +185,134 @@ public Vector3 NextDirection3D()
194185 #endregion
195186
196187 }
188+
189+ /// <summary>
190+ /// Noise based random number generator
191+ /// Allows for fast random-access within a sequence of generated numbers
192+ /// Based on GDC 2017 talk and code by Squirrel Eiserloh
193+ /// </summary>
194+ public class RandomGenerator : IRandomAccessRandomNumberGenerator
195+ {
196+ private RandomGeneratorStruct _randomGeneratorStruct ;
197+
198+ /// <summary>
199+ /// Initialize random number generator with a given seed
200+ /// </summary>
201+ /// <param name="seed">see for the random number generator</param>
202+ public RandomGenerator ( uint seed )
203+ {
204+ _randomGeneratorStruct = new RandomGeneratorStruct ( seed ) ;
205+ }
206+
207+ /// <summary>
208+ /// Seeds using DateTime.UtcNow.Ticks
209+ /// </summary>
210+ public RandomGenerator ( )
211+ {
212+ _randomGeneratorStruct = new RandomGeneratorStruct ( ( uint ) DateTime . UtcNow . Ticks ) ;
213+ }
214+
215+ /// <summary>
216+ /// Current position in the number generator sequence
217+ /// </summary>
218+ public int Position
219+ {
220+ get => _randomGeneratorStruct . Position ;
221+ set => _randomGeneratorStruct . Position = value ;
222+ }
223+
224+ /// <summary>
225+ /// Current Seed
226+ /// </summary>
227+ public uint Seed => _randomGeneratorStruct . Seed ;
228+
229+ /// <summary>
230+ /// Reseed the number generator with a new seed and position
231+ /// </summary>
232+ /// <param name="seed">seed</param>
233+ /// <param name="position">position in number generation sequence</param>
234+ public void ReSeed ( uint seed , int position = 0 ) => _randomGeneratorStruct . ReSeed ( seed , position ) ;
235+
236+ #region Rand Methods
237+
238+ /// <summary>
239+ /// Next random unsigned integer
240+ /// </summary>
241+ /// <returns>next random unsigned integer in the current sequence</returns>
242+ public uint NextUint ( ) => _randomGeneratorStruct . NextUint ( ) ;
243+
244+ /// <summary>
245+ /// Next random unsigned short
246+ /// </summary>
247+ /// <returns>next random unsigned short in the current sequence</returns>
248+ public ushort NextUshort ( ) => _randomGeneratorStruct . NextUshort ( ) ;
249+
250+ /// <summary>
251+ /// Next random byte
252+ /// </summary>
253+ /// <returns>next random byte in the current sequence</returns>
254+ public byte NextByte ( ) => _randomGeneratorStruct . NextByte ( ) ;
255+
256+ /// <summary>
257+ /// Next random integer
258+ /// </summary>
259+ /// <returns>next random integer in the current sequence</returns>
260+ public int NextInt ( ) => _randomGeneratorStruct . NextInt ( ) ;
261+
262+ /// <summary>
263+ /// Next random short
264+ /// </summary>
265+ /// <returns>next random short in the current sequence</returns>
266+ public short NextShort ( ) => _randomGeneratorStruct . NextShort ( ) ;
267+
268+ /// <summary>
269+ /// Next random float in the range 0 to 1
270+ /// </summary>
271+ /// <returns>Next random float in the range 0 to 1 (inclusive, inclusive)</returns>
272+ public float NextFloatZeroToOne ( ) => _randomGeneratorStruct . NextFloatZeroToOne ( ) ;
273+
274+ /// <summary>
275+ /// Next random float in the range -1 to 1
276+ /// </summary>
277+ /// <returns>Next random float in the range -1 to 1 (inclusive, inclusive)</returns>
278+ public float NextFloatNegOneToOne ( ) => _randomGeneratorStruct . NextFloatNegOneToOne ( ) ;
279+
280+ /// <summary>
281+ /// Next random float in the range min to max
282+ /// </summary>
283+ /// <param name="min">min return value</param>
284+ /// <param name="max">max return value</param>
285+ /// <returns>Next random float in the range min to max (inclusive, inclusive)</returns>
286+ public float NextFloatRange ( float min , float max ) => _randomGeneratorStruct . NextFloatRange ( min , max ) ;
287+
288+ /// <summary>
289+ /// Next random integer in the range min to max
290+ /// </summary>
291+ /// <param name="min">min return value</param>
292+ /// <param name="max">max return value</param>
293+ /// <returns>Next random int in the range min to max (inclusive, inclusive)</returns>
294+ public int NextIntRange ( int min , int max ) => _randomGeneratorStruct . NextIntRange ( min , max ) ;
295+
296+ /// <summary>
297+ /// Rolls for a true for false with the given probability
298+ /// </summary>
299+ /// <param name="probabilityOfReturningTrue">probably that this function will return true</param>
300+ /// <returns>true or false</returns>
301+ public bool RollChance ( float probabilityOfReturningTrue ) =>
302+ _randomGeneratorStruct . RollChance ( probabilityOfReturningTrue ) ;
303+
304+ /// <summary>
305+ /// Next uniformly distributed randomized 2d direction
306+ /// </summary>
307+ /// <returns>Random Normalized Vector2</returns>
308+ public Vector2 NextDirection2D ( ) => _randomGeneratorStruct . NextDirection2D ( ) ;
309+
310+ /// <summary>
311+ /// Next uniformly distributed randomized 3d direction
312+ /// </summary>
313+ /// <returns>Random Normalized Vector3</returns>
314+ public Vector3 NextDirection3D ( ) => _randomGeneratorStruct . NextDirection3D ( ) ;
315+
316+ #endregion
317+ }
197318}
0 commit comments