如何在C#/Unity和Python之间同步PRNG? [英] How to sync a PRNG between C#/Unity and Python?

查看:64
本文介绍了如何在C#/Unity和Python之间同步PRNG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Unity/C#实现的游戏,它使用内置的PRNG( UnityEngine.Random )生成简单的环境.我正在尝试在Python 3中重新实现环境生成过程.我需要同步随机数生成器,以便在提供相同种子时,Unity中的实际游戏和Python重新实现会生成完全相同的环境.同步两个平台的最佳方法是什么?

I have a game implemented in Unity/C# that generates simple environments using the built-in PRNG (UnityEngine.Random). I am trying to reimplement the environment generation procedure in Python 3. I need the random number generators to be synchronized so that when provided with the same seed, the actual game in Unity and the Python reimplementation generate the exact same environment. What would be the best approach to synchronizing the two platforms?

到目前为止,我已经考虑过一些解决方案:

Some solutions I have considered so far:

  • 试图在C#中重新实现Python的默认随机数生成器( random ).源代码可用,但是相当长,因此在实践中可能难以实现.
  • 尝试在Python中重新实现 UnityEngine.Random .但是,我没有任何源代码,即使我知道所使用的PRNG的类,也无法保证我能够完全相同地成功重新实现它.
  • 在两者中均实施相同的PRNG.但是,我不知道在我的用例中,PRNG有什么好的选择.我基本上需要看起来随机的东西(尽管它不一定是安全的),并且组装时间不超过一两个小时.维基百科的PRNG列表很长,我不知道实现每个PRNG的困难.
  • 或者也许其他人在某个时候做了这个……?不过,我在网上找不到任何东西.
  • Attempt to reimplement Python's default random number generator (random) in C#. The source code is available, but fairly long so may difficult to implement in practice.
  • Attempt to reimplement UnityEngine.Random in Python. However, I don't have any source code, and even if I knew the class of PRNG used, there is no guarantee that I will be able to successfully reimplement it exactly the same.
  • Implement the same PRNG in both. However, I don't know what a good option for a PRNG is for my use case. I basically need something that looks random (though it doesn't have to be secure) and doesn't take more than an hour or two to put together. Wikipedia has a long list of PRNGs, and I have no idea the difficulty in implementing each of them.
  • Or maybe someone else has done this at some point...? I couldn't find anything online, though.

关于最佳方法的任何建议,或者我可以在C#和Python中轻松实现的简单PRNG?

Any suggestions on the best approach, or a simple PRNG I can implement easily in both C# and Python?

谢谢!

推荐答案

通常,同步"的最佳方法是两种使用不同语言的程序之间的PRNG是要在两种语言中实现相同的PRNG.

In general, the best way to "sync" PRNGs between two programs in different languages is to implement the same PRNG in both languages.

出于您的目的,如果您仅希望看起来随机的东西(尽管它不一定安全)",则线性同余生成器(LCG)是一种简单的PRNG.在C#和Python中都很难实现这种生成器.

For your purposes, a linear congruential generator (LCG) is a simple PRNG if you only want "something that looks random (though it doesn't have to be secure)". This kind of generator is trivial to implement in both C# and Python.

下面的32位LCG(其中 x 是种子)是一个示例,其中包括许多其他可能性:

One example, among many other possibilities, is the following 32-bit LCG (where x is the seed):

C#:

// Generate the next x from the current one.
unchecked {
    // NOTE: x is an `int`
    x = ((int)0xadb4a92d * x) + 9999999;
}

Python:

# Generate the next x from the current one.
x = ((0xadb4a92d * x) + 9999999) & 0xFFFFFFFF

有关其他参数的选择,请参见Steele和Vigna的最新论文的第8节并对涉及LCG的理论进行了回顾.

See section 8 of the very very recent paper by Steele and Vigna for other parameter choices as well as a review of the theory involving LCGs.

但是,LCG远非完美.(例如,上面的LCG产生具有低位低位的 x ,因此,例如,每个其他 x 是奇数,而每个其他 x 甚至是.)通常,LCG,特别是那些带有32位种子或其他短种子的LCG,在许多情况下都不适合,包括科学工作或信息安全.如果您想选择PRNG,我会列出其中的许多.

However, LCGs are far from perfect. (For instance, the above LCG produces x's with weak low bits, so that, e.g., every other x is odd and every other x is even.) And in general, LCGs, especially those with 32-bit seeds or other short seeds, are far from appropriate for many situations, including scientific work or information security. In case you want another choice for a PRNG, I list many of them.

这篇关于如何在C#/Unity和Python之间同步PRNG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆