PC-20230316NUNE\Administrator 6da2f9e691 提交
2024-10-16 20:41:40 +08:00

62 lines
1.5 KiB
C#

using System;
using JNGame.Math;
using UnityEngine;
namespace Plugins.JNGame.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++;
}
}
}