// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; namespace osu.Game.Utils { /// /// Provides a fast stateless function that can be used in randomly-looking visual elements. /// public static class StatelessRNG { private static ulong mix(ulong x) { unchecked { x ^= x >> 33; x *= 0xff51afd7ed558ccd; x ^= x >> 33; x *= 0xc4ceb9fe1a85ec53; x ^= x >> 33; return x; } } /// /// Generate a random 64-bit unsigned integer from given seed. /// /// /// The seed value of this random number generator. /// /// /// The series number. /// Different values are computed for the same seed in different series. /// public static ulong NextULong(int seed, int series = 0) { unchecked { ulong combined = ((ulong)(uint)series << 32) | (uint)seed; // The xor operation is to not map (0, 0) to 0. return mix(combined ^ 0x12345678); } } /// /// Generate a random integer in range [0, maxValue) from given seed. /// /// /// The number of possible results. /// /// /// The seed value of this random number generator. /// /// /// The series number. /// Different values are computed for the same seed in different series. /// public static int NextInt(int maxValue, int seed, int series = 0) { ArgumentOutOfRangeException.ThrowIfNegativeOrZero(maxValue); return (int)(NextULong(seed, series) % (ulong)maxValue); } /// /// Compute a random floating point value between 0 and 1 (excluding 1) from given seed and series number. /// /// /// The seed value of this random number generator. /// /// /// The series number. /// Different values are computed for the same seed in different series. /// public static float NextSingle(int seed, int series = 0) => (float)(NextULong(seed, series) & ((1 << 24) - 1)) / (1 << 24); // float has 24-bit precision /// /// Compute a random floating point value between and from given seed and series number. /// public static float NextSingle(float min, float max, int seed, int series = 0) => min + NextSingle(seed, series) * (max - min); } }