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

查看:19
本文介绍了如何在 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,我不知道实现它们的难度.
  • 或者也许其他人在某个时候这样做过......?不过,我在网上找不到任何东西.
  • 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;
}

蟒蛇:

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

有关其他参数选择,请参见最近 论文的第 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天全站免登陆