Mersenne Twister:播种和播种可视化

发布于 2024-08-11 19:49:22 字数 1116 浏览 5 评论 0原文

我正在使用从 CenterSpace 下载的 Mersenne Twister 的 C# 实现。我有两个问题:

  1. 无论我如何播种算法,它都无法通过 DieHard 测试,我的意思是我得到了很多 p 值的 1 和 0。另外,我对 269 个 p 值的 KStest 为 0。好吧,我无法完全解释 p 值,但我认为结果中的一些 1 和 0 是个坏消息。
  2. 我被要求直观地显示数字的随机性。所以我在生成数字时绘制它们,这看起来根本不是随机的。这是几秒钟后几秒钟后。正如您在第二个屏幕截图中看到的,数字落在一些平行线上。我尝试了不同的算法将数字映射到点。它们都会产生平行线,但角度不同!这就是我将数字映射到这些屏幕截图的点的方式:new Point(number % _canvasWidth, number % _canvasHeight)。正如您可能猜到的,视觉结果取决于表单的宽度和高度,这是< /a> 灾难性的结果。

以下是我尝试为算法提供种子的几种方法:

  1. 用户输入。我输入一些数字作为 int 数组来为算法提供种子。
  2. 算法本身生成的随机数!!
  3. new Guid().GetHashCode() 数组

我在这里缺少什么?我应该如何播种算法?我怎样才能让它通过死硬派?

I am using a C# implementation of Mersenne Twister I downloaded from CenterSpace. I have two problems with it:

  1. No matter how I seed the algorithm it does not pass DieHard tests, and by that I mean I get quite a lot of 1s and 0s for p-value. Also my KStest on 269 p-values is 0. Well, I cannot quite interpret p-value, but I think a few 1s and 0s in the result is bad news.
  2. I have been asked to visually show the randomness of the numbers. So I plot the numbers as they are generated, and this does not seem random at all. Here is two screenshots of the result after a few seconds and a few seconds later. As you can see in the second screenshot the numbers fall on some parallel lines. I have tried different algorithms to map numbers to points. They all result in parallel lines, but with different angles! This is how I mapped numbers to points for these screenshots: new Point(number % _canvasWidth, number % _canvasHeight). As you may guess, the visual result depends on the form's width and height, and this is a disasterous result.

Here is a few ways I tried to seed the algorithm:

  1. User entry. I enter some numbers to seed the algorithm as an int array.
  2. Random numbers generated by the algorithm itself!!
  3. An array of new Guid().GetHashCode()

What am I missing here? How should I seed the algorithm? How can I get it pass the DieHard?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人事已非 2024-08-18 19:49:22

虽然我无法回答你的第一点,但第二个问题与你如何计算要利用的点有关。具体来说,

x = number % _canvasWidth;
y = number % _canvasHeight;

将为您提供一个“图案”,该图案在某种程度上与您正在绘制的窗口的纵横比相对应。例如,如果 _canvasWidth_canvasHeight 相等,则您始终会在单对角线上绘制 xy > 总是一样的。那么这种图形表示在这种情况下是不合适的。

获取 RNG 输出的 N 位并使用一半作为 x 坐标,另一半作为 y 坐标怎么样?对于那些超出窗口范围的位,您可能需要考虑两个选项:

  1. 不要绘制它们(或将它们绘制到屏幕外)
  2. 执行线性插值以将位范围映射到窗口的宽度/

高度选项应该为您提供更具代表性的随机数生成器比特图片。祝你好运!

While I cannot speak to your first point, the second problem has to do with how you are computing the points to draw on. Specifically,

x = number % _canvasWidth;
y = number % _canvasHeight;

will give you a "pattern" that corresponds somewhat to the aspect ratio of the window you are drawing to. For example, if _canvasWidth and _canvasHeight were equal, you would always draw on a single diagonal line as x and y would always be the same. This graphical representation wouldn't be appropriate in this case, then.

What about taking the N bits of the RNG output and using half for the x coordinate and the other half for the y coordinate? For those bits that fall out of the bounds of your window you might want to consider two options:

  1. Don't draw them (or draw them offscreen)
  2. Perform a linear interpolation to map the range of bits to the width/height of your window

Either option should give you a more representative picture of the bits you are getting our of your random number generator. Good luck!

别把无礼当个性 2024-08-18 19:49:22

通过为每个 x 和 y 坐标生成一个新的随机数,您的条纹点绘图问题应该可以轻松解决。尝试为 x 和 y 重复使用单个生成的数字基本上是不成熟的优化,但如果您确实走这条路,请确保从数字中为每个数字提取不同的位;照原样,x=n%width;y=n%height 为您提供了 x 和 y 之间的巨大相关性,如图像中所示。

多年来我一直在使用各种 C++ Mersenne Twister 实现(最近 boost's) 生成随机 积分并且没有任何困难(与种子相关或其他方面)。它确实是一个出色的生成器。

Your stripy point-plotting problem should easily be fixed by generating a new random number for each of the x and y coordinates. Trying to reuse a single generated number for x and y is basically premature optimization, but if you do go down that route, make sure you extract different bits for each from the number; as is, x=n%width;y=n%height gives you enormous correlation between x and y, as can be seen in your images.

I've been using various C++ Mersenne Twister implementations for years (most recently boost's) to generate random points and had no difficulties with it (seed related or otherwise). It really is a superb generator.

Hello爱情风 2024-08-18 19:49:22

真正的随机数生成不能用数学函数来完成。如果拥有真正的随机数很重要,请获取硬件随机数生成器。我开发了真钱在线扑克游戏——这样的硬件是确保数字没有规律的唯一方法。

如果针对 Linux 环境,/dev/random 和 /dev/urandom 伪设备 会执行比数学生成器好得多,因为它们包含代表硬件活动的随机数。

True random number generation cannot be done with a mathematical function. If it's important to have truly random numbers, get a hardware random number generator. I've developed real money online poker games—such hardware is the only way to be confident there are no patterns in the numbers.

If targeting a Linux environment, the /dev/random and /dev/urandom pseudo devices do a lot better than a mathematical generator, since they incorporate random numbers representing hardware activity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文