Files
TEngine/Assets/TEngine/Runtime/GameFramework/Utility/Utility.Random.cs
ALEXTANG aeb1ff29f3 Update
Update
2023-04-01 17:22:44 +08:00

73 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
namespace TEngine
{
public static partial class Utility
{
/// <summary>
/// 随机相关的实用函数。
/// </summary>
public static class Random
{
private static System.Random s_Random = new System.Random((int)DateTime.UtcNow.Ticks);
/// <summary>
/// 设置随机数种子。
/// </summary>
/// <param name="seed">随机数种子。</param>
public static void SetSeed(int seed)
{
s_Random = new System.Random(seed);
}
/// <summary>
/// 返回非负随机数。
/// </summary>
/// <returns>大于等于零且小于 System.Int32.MaxValue 的 32 位带符号整数。</returns>
public static int GetRandom()
{
return s_Random.Next();
}
/// <summary>
/// 返回一个小于所指定最大值的非负随机数。
/// </summary>
/// <param name="maxValue">要生成的随机数的上界随机数不能取该上界值。maxValue 必须大于等于零。</param>
/// <returns>大于等于零且小于 maxValue 的 32 位带符号整数,即:返回值的范围通常包括零但不包括 maxValue。不过如果 maxValue 等于零,则返回 maxValue。</returns>
public static int GetRandom(int maxValue)
{
return s_Random.Next(maxValue);
}
/// <summary>
/// 返回一个指定范围内的随机数。
/// </summary>
/// <param name="minValue">返回的随机数的下界(随机数可取该下界值)。</param>
/// <param name="maxValue">返回的随机数的上界随机数不能取该上界值。maxValue 必须大于等于 minValue。</param>
/// <returns>一个大于等于 minValue 且小于 maxValue 的 32 位带符号整数,即:返回的值范围包括 minValue 但不包括 maxValue。如果 minValue 等于 maxValue则返回 minValue。</returns>
public static int GetRandom(int minValue, int maxValue)
{
return s_Random.Next(minValue, maxValue);
}
/// <summary>
/// 返回一个介于 0.0 和 1.0 之间的随机数。
/// </summary>
/// <returns>大于等于 0.0 并且小于 1.0 的双精度浮点数。</returns>
public static double GetRandomDouble()
{
return s_Random.NextDouble();
}
/// <summary>
/// 用随机数填充指定字节数组的元素。
/// </summary>
/// <param name="buffer">包含随机数的字节数组。</param>
public static void GetRandomBytes(byte[] buffer)
{
s_Random.NextBytes(buffer);
}
}
}
}