mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-26 03:14:47 +00:00
61 lines
1.5 KiB
C#
61 lines
1.5 KiB
C#
using System;
|
|
using JNGame.Math;
|
|
|
|
namespace JNGame.Runtime.Util
|
|
{
|
|
public static class RandomUtil
|
|
{
|
|
|
|
public static Func<LFloat> SyncRandom(int seed = 1000000,int start = 10)
|
|
{
|
|
|
|
// 线性同余生成器的参数
|
|
// const int a = 1103515245;
|
|
// const int increment = 16807;
|
|
|
|
for (var i = 0; i < start; i++)
|
|
{
|
|
seed = (110351* seed + 16807) % 214748;
|
|
}
|
|
|
|
return () =>
|
|
{
|
|
seed = (110351 * seed + 16807) % 214748;
|
|
// 返回随机浮点数
|
|
return LMath.Abs((LFloat)seed / 214748);
|
|
};
|
|
|
|
}
|
|
|
|
public static Func<LFloat,LFloat,LFloat> SyncRandomFloat(int seed = 1000000,int start = 10)
|
|
{
|
|
|
|
var nRandom = SyncRandom(seed, start);
|
|
|
|
return (LFloat min,LFloat max) =>
|
|
{
|
|
var random = nRandom();
|
|
return random * (max - min) + min;
|
|
};
|
|
|
|
}
|
|
|
|
public static Func<int,int,int> SyncRandomInt(int seed = 2000000,int start = 10)
|
|
{
|
|
|
|
var nRandom = SyncRandom(seed, start);
|
|
return (min, max) =>
|
|
{
|
|
var random = nRandom();
|
|
return LMath.Round(random * (max - min) + min);
|
|
};
|
|
|
|
}
|
|
|
|
public static Func<int> Next(int start)
|
|
{
|
|
return () => start++;
|
|
}
|
|
|
|
}
|
|
} |