mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-26 11:24:46 +00:00
62 lines
1.6 KiB
C#
62 lines
1.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace Plugins.JNGame.Util
|
|
{
|
|
public static class RandomUtil
|
|
{
|
|
|
|
public static Func<float> SyncRandom(int seed = 1000000,int start = 10)
|
|
{
|
|
|
|
// 线性同余生成器的参数
|
|
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; // 返回随机浮点数
|
|
};
|
|
|
|
}
|
|
|
|
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 = 2000000,int start = 10)
|
|
{
|
|
|
|
var nRandom = SyncRandom(seed, start);
|
|
return (min, max) =>
|
|
{
|
|
var random = nRandom();
|
|
return (int)Math.Round(random * (max - min) + min);
|
|
};
|
|
|
|
}
|
|
|
|
public static Func<int> Next(int start)
|
|
{
|
|
return () => start++;
|
|
}
|
|
|
|
}
|
|
} |