I need to migrate some C# code to Python. The original code makes use of the Random class. The migrated code must be cycle-accurate (namely consecutive calls to Next() must produce the same results in both codes). Some questions:
- Is there a call-equivalent to C#’s
Randomin Python? - Alliteratively, assuming I can modify both sources, is there a pseudo-random library which works with both C# and Python?
I don’t know of any library that is available for both Python and C# and which generates the same random numbers for both. However, you may be able to take advantage of IronPython. The default
Randomimplementation differs between IronPython and CPython, but theWichmannHillclass does not.You can use C# to instantiate the
WichmannHillclass in IronPython and get the same values as CPython for the same seed. Alternatively, you can implement the Wichmann-Hill algorithm in C# relatively easily by translating the Python code inrandom.py.Another option is to take the CPython implementation of
Random‘s Mersenne Twister algorithm and translate that to C# to get identical results.