62 lines
1.5 KiB
C#
Raw Normal View History

2024-08-17 14:27:18 +08:00
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++;
}
}
}