This commit is contained in:
PC-20230316NUNE\Administrator
2024-01-30 19:22:27 +08:00
parent 09db51f67b
commit a0c687b1ed
114 changed files with 18393 additions and 13605 deletions

View File

@@ -1,31 +1,55 @@
using System;
using UnityEngine;
namespace Plugins.JNGame.Util
{
public static class RandomUtil
{
public static Func<double> SyncRandom(int seed,int start = 1)
public static Func<float> SyncRandom(int seed = 1000000,int start = 10)
{
for(var i = 0; i < start; i++){
seed = (seed * 9301 + 49297) % 233280;
}
// 线性同余生成器的参数
const int a = 1103515245;
const int m = 2147483647; // 2^31 - 1
const int increment = 16807;
for (var i = 0; i < start; i++)
{
seed = (a * seed + increment) % m;
}
return () =>
{
seed = (a * seed + increment) % m;
var rnd = Math.Abs((float)seed / m); // 转换为0.0到1.0之间的数
return rnd; // 返回随机浮点数
};
return () =>
{
seed = (seed * 9301 + 49297) % 233280;
var rnd = seed / 233280.0;
return 0 + rnd * (1 - 0);
};
}
public static Func<float,float,float> SyncRandomFloat(int seed = 1000000,int start = 10)
{
var nRandom = SyncRandom(seed, start);
return (float min,float max) =>
{
var random = nRandom();
return random * (max - min) + min;
};
}
public static Func<int,int,int> SyncRandomInt(int seed,int start = 1)
public static Func<int,int,int> SyncRandomInt(int seed = 2000000,int start = 10)
{
var nRandom = SyncRandom(seed, start);
return (min, max) => (int)(Math.Round(nRandom() * (max - min)) + min);
return (min, max) =>
{
var random = nRandom();
return (int)Math.Round(random * (max - min) + min);
};
}