mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
提交
This commit is contained in:
@@ -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);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user