Nej*_*chy 16 .net c# random .net-1.1
我正在迁移一个用于从.NET Framework 1.1解码到.NET Framework 4的方法.我注意到Random的实现已更改.因此,给定相同的种子,Random.NextBytes返回不同的结果.
所以,如果我运行以下代码.
byte[] bytes = new byte[4];
System.Random random = new System.Random(50);
random.NextBytes(bytes);
for(int i=0; i< bytes.Length; i++)
{
Console.WriteLine("bytes[" + i + "] = " + bytes[i]);
}
Run Code Online (Sandbox Code Playgroud)
在.NET Framework 1.1下,它返回:
bytes[0] = 216
bytes[1] = 124
bytes[2] = 183
bytes[3] = 58
Run Code Online (Sandbox Code Playgroud)
在.NET framework 4下,它返回:
bytes[0] = 154
bytes[1] = 49
bytes[2] = 183
bytes[3] = 48
Run Code Online (Sandbox Code Playgroud)
解决此问题的最佳方法是什么?
Gre*_*g D 27
这不是问题Random,它完全可以满足其记录的界面.这是您的软件依赖于实现细节的问题.从这个错误中吸取教训,不要再这样做了.
至于解决问题,您可以实现自己的1.1版伪随机数生成版本进行解码,然后实现一个新的编码/解码算法,该算法不依赖于新的不稳定行为(例如执行Random或GetHashCode)该软件的版本.
Wil*_*ill 17
您可以使用Reflector从1.1 mscorlib复制Random类.
public class Random1_1
{
// Fields
private int inext;
private int inextp;
private const int MBIG = 0x7fffffff;
private const int MSEED = 0x9a4ec86;
private const int MZ = 0x0;
private int[] SeedArray;
// Methods
public Random1_1()
: this(Environment.TickCount)
{
}
public Random1_1(int Seed)
{
this.SeedArray = new int[0x38];
int num2 = 0x9a4ec86 - Math.Abs(Seed);
this.SeedArray[0x37] = num2;
int num3 = 0x1;
for (int i = 0x1; i < 0x37; i++)
{
int index = (0x15 * i) % 0x37;
this.SeedArray[index] = num3;
num3 = num2 - num3;
if (num3 < 0x0)
{
num3 += 0x7fffffff;
}
num2 = this.SeedArray[index];
}
for (int j = 0x1; j < 0x5; j++)
{
for (int k = 0x1; k < 0x38; k++)
{
this.SeedArray[k] -= this.SeedArray[0x1 + ((k + 0x1e) % 0x37)];
if (this.SeedArray[k] < 0x0)
{
this.SeedArray[k] += 0x7fffffff;
}
}
}
this.inext = 0x0;
this.inextp = 0x15;
Seed = 0x1;
}
public virtual int Next()
{
return (int)(this.Sample() * 2147483647.0);
}
public virtual int Next(int maxValue)
{
if (maxValue < 0x0)
{
throw new ArgumentOutOfRangeException("maxValue");
}
return (int)(this.Sample() * maxValue);
}
public virtual int Next(int minValue, int maxValue)
{
if (minValue > maxValue)
{
throw new ArgumentOutOfRangeException("minValue");
}
int num = maxValue - minValue;
if (num < 0x0)
{
long num2 = maxValue - minValue;
return (((int)((long)(this.Sample() * num2))) + minValue);
}
return (((int)(this.Sample() * num)) + minValue);
}
public virtual void NextBytes(byte[] buffer)
{
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
for (int i = 0x0; i < buffer.Length; i++)
{
buffer[i] = (byte)(this.Sample() * 256.0);
}
}
public virtual double NextDouble()
{
return this.Sample();
}
protected virtual double Sample()
{
int inext = this.inext;
int inextp = this.inextp;
if (++inext >= 0x38)
{
inext = 0x1;
}
if (++inextp >= 0x38)
{
inextp = 0x1;
}
int num = this.SeedArray[inext] - this.SeedArray[inextp];
if (num < 0x0)
{
num += 0x7fffffff;
}
this.SeedArray[inext] = num;
this.inext = inext;
this.inextp = inextp;
return (num * 4.6566128752457969E-10);
}
}
Run Code Online (Sandbox Code Playgroud)
经过测试,它可以提供所需的输出.
| 归档时间: |
|
| 查看次数: |
1767 次 |
| 最近记录: |