2025-05-01 10:45:31 +08:00
|
|
|
|
using Obfuz.Emit;
|
2025-04-22 08:13:58 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Obfuz.Utils
|
|
|
|
|
{
|
|
|
|
|
public class RandomWithKey : IRandom
|
|
|
|
|
{
|
2025-05-06 09:13:24 +08:00
|
|
|
|
private const long a = 1664525;
|
|
|
|
|
private const long c = 1013904223;
|
|
|
|
|
private const long m = 4294967296; // 2^32
|
2025-04-22 08:13:58 +08:00
|
|
|
|
|
2025-05-11 08:46:01 +08:00
|
|
|
|
private readonly int[] _key;
|
2025-04-22 08:13:58 +08:00
|
|
|
|
|
|
|
|
|
private int _nextIndex;
|
|
|
|
|
|
2025-05-06 09:13:24 +08:00
|
|
|
|
private int _seed;
|
|
|
|
|
|
2025-05-13 08:56:19 +08:00
|
|
|
|
public RandomWithKey(int[] key, int seed)
|
2025-04-22 08:13:58 +08:00
|
|
|
|
{
|
2025-05-13 08:56:19 +08:00
|
|
|
|
_key = key;
|
2025-05-06 09:13:24 +08:00
|
|
|
|
_seed = seed;
|
2025-04-22 08:13:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int NextInt(int min, int max)
|
|
|
|
|
{
|
|
|
|
|
return min + NextInt(max - min);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int NextInt(int max)
|
|
|
|
|
{
|
|
|
|
|
return (int)((uint)NextInt() % (uint)max);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-11 09:17:04 +08:00
|
|
|
|
private int GetNextSalt()
|
2025-04-22 08:13:58 +08:00
|
|
|
|
{
|
|
|
|
|
if (_nextIndex >= _key.Length)
|
|
|
|
|
{
|
|
|
|
|
_nextIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
return _key[_nextIndex++];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int NextInt()
|
|
|
|
|
{
|
2025-05-06 09:13:24 +08:00
|
|
|
|
_seed = (int)((a * _seed + c) % m);
|
2025-05-11 09:17:04 +08:00
|
|
|
|
return _seed ^ GetNextSalt();
|
2025-04-22 08:13:58 +08:00
|
|
|
|
}
|
2025-04-23 10:28:27 +08:00
|
|
|
|
|
|
|
|
|
public long NextLong()
|
|
|
|
|
{
|
|
|
|
|
return ((long)NextInt() << 32) | (uint)NextInt();
|
|
|
|
|
}
|
2025-04-22 08:13:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|