mirror of
https://github.com/osukey/osukey.git
synced 2025-07-01 16:29:58 +09:00
Rename functions, and add NextInt.
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||||
// See the LICENCE file in the repository root for full licence text.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace osu.Game.Utils
|
namespace osu.Game.Utils
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -22,7 +24,7 @@ namespace osu.Game.Utils
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Compute an integer from given seed and series number.
|
/// Generate a random 64-bit unsigned integer from given seed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="seed">
|
/// <param name="seed">
|
||||||
/// The seed value of this random number generator.
|
/// The seed value of this random number generator.
|
||||||
@ -31,11 +33,39 @@ namespace osu.Game.Utils
|
|||||||
/// The series number.
|
/// The series number.
|
||||||
/// Different values are computed for the same seed in different series.
|
/// Different values are computed for the same seed in different series.
|
||||||
/// </param>
|
/// </param>
|
||||||
public static ulong Get(int seed, int series = 0) =>
|
public static ulong NextUlong(int seed, int series = 0)
|
||||||
unchecked(mix(((ulong)(uint)series << 32) | ((uint)seed ^ 0x12345678)));
|
{
|
||||||
|
unchecked
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var combined = ((ulong)(uint)series << 32) | (uint)seed;
|
||||||
|
// The xor operation is to not map (0, 0) to 0.
|
||||||
|
return mix(combined ^ 0x12345678);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Compute a floating point value between 0 and 1 (excluding 1) from given seed and series number.
|
/// Generate a random integer in range [0, maxValue) from given seed.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="maxValue">
|
||||||
|
/// The number of possible results.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="seed">
|
||||||
|
/// The seed value of this random number generator.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="series">
|
||||||
|
/// The series number.
|
||||||
|
/// Different values are computed for the same seed in different series.
|
||||||
|
/// </param>
|
||||||
|
public static int NextInt(int maxValue, int seed, int series = 0)
|
||||||
|
{
|
||||||
|
if (maxValue <= 0) throw new ArgumentOutOfRangeException(nameof(maxValue));
|
||||||
|
|
||||||
|
return (int)(NextUlong(seed, series) % (ulong)maxValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Compute a random floating point value between 0 and 1 (excluding 1) from given seed and series number.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="seed">
|
/// <param name="seed">
|
||||||
/// The seed value of this random number generator.
|
/// The seed value of this random number generator.
|
||||||
@ -44,7 +74,7 @@ namespace osu.Game.Utils
|
|||||||
/// The series number.
|
/// The series number.
|
||||||
/// Different values are computed for the same seed in different series.
|
/// Different values are computed for the same seed in different series.
|
||||||
/// </param>
|
/// </param>
|
||||||
public static float GetSingle(int seed, int series = 0) =>
|
public static float NextSingle(int seed, int series = 0) =>
|
||||||
(float)(Get(seed, series) & ((1 << 24) - 1)) / (1 << 24); // float has 24-bit precision
|
(float)(NextUlong(seed, series) & ((1 << 24) - 1)) / (1 << 24); // float has 24-bit precision
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user