我需要将一些C#代码迁移到Python中。原始代码使用了 Random
类。迁移后的代码必须保证循环精度(即连续调用 Next()
方法在两种代码中产生相同的结果)。以下是一些问题:
- 在Python中,是否有与 C# 的
Random
相当的调用等效方法? - 换句话说,假设我可以修改两个源代码,是否有一个伪随机库适用于 C# 和 Python 两种语言?
https://gist.github.com/BadStreff/541cf2e6953b3c666f83127a1d4f6a47
from ctypes import *
# implemented from:
# http://referencesource.microsoft.com/#mscorlib/system/random.cs,dec894a7e816e665
class Random(object):
def __init__(self, seed):
self.seed = c_int(seed).value
self.MBIG = 2147483647
self.MMIN = -2147483648
self.MZ = 0
self.MSEED = 161803398
self.SeedArray = [0] * 56
if seed == self.MMIN:
subtraction = self.MBIG
else:
subtraction = abs(seed)
mj = c_int(self.MSEED - subtraction).value
self.SeedArray[55] = mj
mk = 1
for i in range(1, 55):
ii = (21 * i) % 55
self.SeedArray[ii] = mk
mk = mj - mk
if mk < 0:
mk += self.MBIG
mj = self.SeedArray[ii]
for k in range(1, 5):
for i in range(1, 56):
self.SeedArray[i] -= self.SeedArray[1 + (i + 30) % 55]
if self.SeedArray[i] < 0:
self.SeedArray[i] = c_int(self.SeedArray[i] + self.MBIG).value
self.inext = 0
self.inextp = 21
self.seed = 1
def InternalSample(self):
locINext = self.inext + 1
locINextp = self.inextp + 1
if locINext >= 56:
locINext = 1
if locINextp >= 56:
locINextp = 1
retVal = c_int(self.SeedArray[locINext] - self.SeedArray[locINextp]).value
if retVal == self.MBIG:
retVal -= 1
if retVal < 0:
retVal = c_int(retVal + self.MBIG).value
self.SeedArray[locINext] = retVal
self.inext = locINext
self.inextp = locINextp
return retVal
def Next(self, minValue=None, maxValue=None):
if minValue == None:
return self.InternalSample()
valRange = maxValue - minValue
if valRange <= self.MBIG:
return int(c_float(self.Sample() * valRange).value) + minValue
else:
return self.GetSampleForLargeRange() * valRange + minValue
def GetSampleRangeForLargeRange(self):
pass
def Sample(self):
s = self.InternalSample()
ret = c_double(s * c_double(1.0/self.MBIG).value).value
# print(f'sample: {s}\nret: {ret}')
return ret
我不知道有哪个库可以同时用于Python和C#,并且为两者生成相同的随机数。但是,您可以利用IronPython。默认的Random
实现在IronPython和CPython之间有所不同,但WichmannHill
类不会。
您可以使用C#在IronPython中实例化WichmannHill
类,并获得与相同种子的CPython相同的值。或者,您可以通过翻译random.py
中的Python代码,相对容易地在C#中实现Wichmann-Hill算法。
另一个选择是采用Random
的Mersenne Twister算法的CPython实现,并将其翻译成C#以获得相同的结果。