/// <summary> /// Creates an instance of the XorShift-Add engine using the provided <paramref name="bitGenerator"/> to initialize the engine's state. /// </summary> /// <param name="bitGenerator">A supplier of bits used to directly determine the new state of the random engine.</param> /// <returns>A newly created instance of the XorShift-Add engine.</returns> /// <seealso cref="IRandom.Seed(IBitGenerator)"/> /// <seealso cref="RandomStateGenerator"/> public static XorShiftAdd Create(IBitGenerator bitGenerator) { var instance = CreateUninitialized(); instance.Seed(bitGenerator); return(instance); }
/// <summary> /// Reseed the XorShift128+ engine with a combination of its current state and the supplied bit generator. /// </summary> /// <param name="bitGenerator">A supplier of bits used, in conjuction with the current state, to directly determine the new state of the random engine.</param> /// <seealso cref="IRandom"/> /// <seealso cref="RandomStateGenerator"/> public override void MergeSeed(IBitGenerator bitGenerator) { int tryCount = 0; do { #if MAKEITRANDOM_OPTIMIZE_FOR_32BIT uint state0 = _state0 ^ bitGenerator.Next32(); uint state1 = _state1 ^ bitGenerator.Next32(); uint state2 = _state2 ^ bitGenerator.Next32(); uint state3 = _state3 ^ bitGenerator.Next32(); if (state0 != 0 || state1 != 0 || state2 != 0 || state3 != 0) { _state0 = state0; _state1 = state1; _state2 = state2; _state3 = state3; return; } #else ulong state0 = _state0 ^ bitGenerator.Next64(); ulong state1 = _state1 ^ bitGenerator.Next64(); if (state0 != 0 || state1 != 0) { _state0 = state0; _state1 = state1; return; } #endif } while (++tryCount < 4); throw new System.ArgumentException("The provided bit generator was unable to generate a non-zero state, which is required by this random engine."); }
/// <summary> /// Creates an instance of a wrapper around the .NET System.Random engine using the provided <paramref name="bitGenerator"/> to initialize the engine's state. /// </summary> /// <param name="bitGenerator">A supplier of bits used to directly determine the new state of the random engine.</param> /// <returns>A newly created instance of the .NET System.Random engine.</returns> /// <seealso cref="IRandom.Seed(IBitGenerator)"/> /// <seealso cref="RandomStateGenerator"/> public static SystemRandom Create(IBitGenerator bitGenerator) { var instance = CreateUninitialized(); instance.Seed(bitGenerator); return(instance); }
/// <summary> /// Creates an instance of the SplitMix64 engine using the provided <paramref name="bitGenerator"/> to initialize the engine's state. /// </summary> /// <param name="bitGenerator">A supplier of bits used to directly determine the new state of the random engine.</param> /// <returns>A newly created instance of the SplitMix64 engine.</returns> /// <seealso cref="IRandom.Seed(IBitGenerator)"/> /// <seealso cref="RandomStateGenerator"/> public static SplitMix64 Create(IBitGenerator bitGenerator) { var instance = CreateUninitialized(); instance.Seed(bitGenerator); return(instance); }
/// <summary> /// Creates an instance of the XoroShiro128+ engine using the provided <paramref name="bitGenerator"/> to initialize the engine's state. /// </summary> /// <param name="bitGenerator">A supplier of bits used to directly determine the new state of the random engine.</param> /// <returns>A newly created instance of the XoroShiro128+ engine.</returns> /// <seealso cref="IRandom.Seed(IBitGenerator)"/> /// <seealso cref="RandomStateGenerator"/> public static XoroShiro128Plus Create(IBitGenerator bitGenerator) { var instance = CreateUninitialized(); instance.Seed(bitGenerator); return(instance); }
/// <summary> /// Reseed the Unity Random engine with the supplied bit generator. /// </summary> /// <param name="bitGenerator">A supplier of bits used to directly determine the new state of the random engine.</param> /// <seealso cref="IRandom"/> /// <seealso cref="RandomStateGenerator"/> public override void Seed(IBitGenerator bitGenerator) { #if UNITY_5_4_OR_NEWER UnityEngine.Random.InitState((int)bitGenerator.Next32()); #else UnityEngine.Random.seed = (int)bitGenerator.Next32(); #endif }
/// <summary> /// Reseed the Unity Random engine with a combination of its current state and the supplied bit generator. /// </summary> /// <param name="bitGenerator">A supplier of bits used, in conjuction with the current state, to directly determine the new state of the random engine.</param> /// <seealso cref="IRandom"/> /// <seealso cref="RandomStateGenerator"/> public override void MergeSeed(IBitGenerator bitGenerator) { #if UNITY_5_4_OR_NEWER IBitGenerator currentState = new RandomStateGenerator(SaveState()); UnityEngine.Random.InitState((int)(currentState.Next32() ^ bitGenerator.Next32())); #else UnityEngine.Random.seed ^= (int)bitGenerator.Next32(); #endif }
/// <summary> /// Creates an instance of the standard random engine using the provided <paramref name="bitGenerator"/> to initialize the engine's state. /// </summary> /// <param name="bitGenerator">A supplier of bits used to directly determine the new state of the random engine.</param> /// <returns>A newly created instance of the standard random engine.</returns> /// <seealso cref="IRandom.Seed(IBitGenerator)"/> /// <seealso cref="RandomStateGenerator"/> public static IRandom CreateStandard(IBitGenerator bitGenerator) { #if !UNITY_5_2 && !UNITY_5_3_OR_NEWER if (_standardCreator == null) { FindStandardCreator(); } #endif var random = (IRandom)_standardCreator.Invoke(null, _standardCreatorParameters); random.Seed(bitGenerator); return(random); }
/// <summary> /// Reseed the XoroShiro128+ engine with a combination of its current state and the supplied bit generator. /// </summary> /// <param name="bitGenerator">A supplier of bits used, in conjuction with the current state, to directly determine the new state of the random engine.</param> /// <seealso cref="IRandom"/> /// <seealso cref="RandomStateGenerator"/> public override void MergeSeed(IBitGenerator bitGenerator) { int tryCount = 0; do { ulong state0 = _state0 ^ bitGenerator.Next64(); ulong state1 = _state1 ^ bitGenerator.Next64(); if (state0 != 0 && state1 != 0) { _state0 = state0; _state1 = state1; return; } } while (++tryCount < 4); throw new System.ArgumentException("The provided bit generator was unable to generate a non-zero state, which is required by this random engine."); }
/// <summary> /// Reseed the XorShift-Add engine with the supplied bit generator. /// </summary> /// <param name="bitGenerator">A supplier of bits used to directly determine the new state of the random engine.</param> /// <seealso cref="IRandom"/> /// <seealso cref="RandomStateGenerator"/> public override void Seed(IBitGenerator bitGenerator) { int tryCount = 0; do { uint state0 = bitGenerator.Next32(); uint state1 = bitGenerator.Next32(); uint state2 = bitGenerator.Next32(); uint state3 = bitGenerator.Next32(); if (state0 != 0 || state1 != 0 || state2 != 0 || state3 != 0) { _state0 = state0; _state1 = state1; _state2 = state2; _state3 = state3; return; } } while (++tryCount < 4); throw new System.ArgumentException("The provided bit generator was unable to generate a non-zero state, which is required by this random engine."); }
/// <summary> /// Reseed the XorShift1024* engine with a combination of its current state and the supplied bit generator. /// </summary> /// <param name="bitGenerator">A supplier of bits used, in conjuction with the current state, to directly determine the new state of the random engine.</param> /// <seealso cref="IRandom"/> /// <seealso cref="RandomStateGenerator"/> public override void MergeSeed(IBitGenerator bitGenerator) { int tryCount = 0; do { ulong[] state = new ulong[16]; bool allZeroes = true; for (int i = 0; i < 16; ++i) { state[i] = _state[i] ^ bitGenerator.Next64(); allZeroes = allZeroes && state[i] == 0; } if (!allZeroes) { _state = state; return; } } while (++tryCount < 4); throw new System.ArgumentException("The provided bit generator was unable to generate a non-zero state, which is required by this random engine."); }
/// <summary> /// Reseed the .NET System.Random engine with the supplied bit generator. /// </summary> /// <param name="bitGenerator">A supplier of bits used to directly determine the new state of the random engine.</param> /// <seealso cref="IRandom"/> /// <seealso cref="RandomStateGenerator"/> public override void MergeSeed(IBitGenerator bitGenerator) { _random = new System.Random((int)(Next32() ^ bitGenerator.Next32())); }
/// <summary> /// Reseed the pseudo-random engine with the supplied bit generator. /// </summary> /// <param name="bitGenerator">A supplier of bits used to directly determine the new state of the random engine.</param> /// <seealso cref="IRandom"/> /// <seealso cref="RandomStateGenerator"/> public abstract void Seed(IBitGenerator bitGenerator);
/// <summary> /// Reseed the SplitMix64 engine with a combination of its current state and the supplied bit generator. /// </summary> /// <param name="bitGenerator">A supplier of bits used, in conjuction with the current state, to directly determine the new state of the random engine.</param> /// <seealso cref="IRandom"/> /// <seealso cref="RandomStateGenerator"/> public override void MergeSeed(IBitGenerator bitGenerator) { _state ^= bitGenerator.Next64(); }
/// <summary> /// Reseed the SplitMix64 engine with the supplied bit generator. /// </summary> /// <param name="bitGenerator">A supplier of bits used to directly determine the new state of the random engine.</param> /// <seealso cref="IRandom"/> /// <seealso cref="RandomStateGenerator"/> public override void Seed(IBitGenerator bitGenerator) { _state = bitGenerator.Next64(); }