Delphi中的Random函数有多可靠

发布于 2024-09-28 09:01:18 字数 183 浏览 5 评论 0原文

我正在编写一个用 Delphi(必须是 Delphi)编写统计测试的程序,我听说随机功能有点奇怪。当程序启动时,您必须调用 randomize 来随机化随机函数的种子。

我想知道随机函数(调用 randomize 后)是否足够随机以进行统计测试,或者是否需要梅森扭曲器?有谁对 random 的实际实现有任何了解,可以告诉我这有多重要?

I am writing a program which write statistical tests in Delphi (must be Delphi) and I've heard that the Random functionality is somewhat odd. You have to call randomize to randomize the seed of the random function when the program starts.

I'm wondering if the random function (after calling randomize) is random enough for statistical tests or a Mersenne twister is needed? Does anyone have any insight into random's actual implementation which can tell me how important this is?

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

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

发布评论

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

评论(9

深海夜未眠 2024-10-05 09:01:19

如果您正在寻求一种以最快的执行时间保证随机数唯一性的方法,About.com 已在 最快的唯一随机数生成器Patrick van Logchem 的实现 被选为获胜者。

If you are seeking a way to guarantee uniqueness of random numbers with the fastest execution time, About.com has created a challenge on Fastest Unique Random Number Generator, and Patrick van Logchem's implementation has been elected as the winner.

岁月静好 2024-10-05 09:01:19

除非您购买一些相对深奥的硬件,否则计算机可以提供的随机数的最佳近似是完全确定性的伪随机序列。一般来说,随机化函数使用一些相对随机的值(通常基于时间,但有时基于鼠标移动 - 我不知道Delphi做了什么)作为提供伪随机序列入口点的种子。如果没有这个,您最终将每次都以相同的顺序返回同一组随机数,这往往会违背使用随机数的初衷。

好吧,我意识到这并不能回答有关可靠性的问题,但它应该给您一些信心,要求您调用 randomize 是一个好的生成器的标志,而不是一个坏的生成器。有大量的统计测试可以显示数字序列的随机性,并且 Delphi 随机数生成器很可能适合多种用途,因为它是一个成熟的产品。

Unless you buy some relatively esoteric hardware, the best approximation to random numbers a computer can provide is a completely deterministic pseudorandom sequence. In general, the randomize function uses some relatively random value (often based on the time, but sometimes on mouse movements - I have no idea what Delphi does) as a seed which provides the entry point to the pseudorandom sequence. Without this, you will end up getting back the same set of random numbers in the same order each time, which tends to defeat the purpose of using random numbers in the first place.

Okay, I realize that this doesn't answer the question about reliability, but it should give you some confidence that requiring you to call randomize is a sign of a good generator rather than of a bad one. There are a bunch of statistical tests which show how random a sequence of numbers is, and it is likely that the Delphi random number generator is suitable for many purposes as it is a mature product.

冰之心 2024-10-05 09:01:19

只是为了增加可能性 - Windows 提供了一系列内置的 密码学函数。如果默认情况下尚未包含的话,可能还有一个适用于它们的 Delphi 包装器。

这些函数中还有一个加密的强随机数生成器< /a>.这是迄今为止您在软件中获得的最好的随机性,因为它基于一长串因素来自行播种。我不确定,但我怀疑它甚至会使用硬件随机数生成器(如果你有的话)。

如果这还不够,您还可以尝试在量子随机位生成器服务注册一些真正随机值。

Just to add to the pool of possibilities - Windows offers a range of built-in Cryptography functions. There probably is a Delphi wrapper for them as well, if it's not already included by default.

Among these functions is also a cryptographically strong random number generator. This is by far the best randomness you will get in software, because it seeds itself based on a very long list of factors. I'm not sure, but I suspect it will even use a hardware random number generator if you have one.

And if that's not enough, you can also try to sign up at the Quantum Random Bit Generator Service for some REALLY random values.

微凉 2024-10-05 09:01:19

来自 Embarcadero 网站:

_lrand 是长随机数生成器函数。 _rand 使用周期为 2^64 的乘法同余随机数生成器来返回 0 到 2^31 - 1 范围内的连续伪随机数。

生成器通过使用参数值为 1 调用 srand 来重新初始化。它可以设置通过使用给定的种子号调用 srand 到新的起点。

From the Embarcadero web site:

_lrand is the long random number generator function. _rand uses a multiplicative congruential random number generator with period 2^64 to return successive pseudo-random numbers in the range from 0 to 2^31 - 1.

The generator is reinitialized by calling srand with an argument value of 1. It can be set to a new starting point by calling srand with a given seed number.

嘴硬脾气大 2024-10-05 09:01:19

如果自从我分析后他们没有改变实现(Delphi 4 IIRC),Delphi PRNG 的实现如下:(

Randseed:=int32(Randseed*$08088405)+1
result:=Randseed*Range shr 32

伪代码/假设乘法是任意大的整数)

If they didn't change the implementation since I analyzed it(Delphi 4 IIRC), the Delphi PRNG is implemented like this:

Randseed:=int32(Randseed*$08088405)+1
result:=Randseed*Range shr 32

(Pseudocode/assume the multiplications are on arbitrarily large integers)

攒眉千度 2024-10-05 09:01:19

返回 0..9 之间的随机数

StrToInt(copy(FloatToStr(Random),4,1))

注意:使用前检查 FloatToStr(Random) 长度或使用小数部分中的任何其他数字...

Return random between 0..9

StrToInt(copy(FloatToStr(Random),4,1))

Note:Check FloatToStr(Random) length before use or use any other digit from the decimal part...

稀香 2024-10-05 09:01:18

Delphi 的 PRNG 与几乎所有编程语言 RTL PRNG 一样,是一个线性同余生成器

对于大多数小规模的事情来说它已经足够好了,但是有一些事情需要注意。特别要注意低阶位:乘法和加法的模式意味着低阶位根本不是很随机。但这通常仅适用于拉出大的 32 位值,然后用 mod 或类似方法截断。使用 Random(10) 提取 0 到 9 之间的值在内部使用整个 32 位范围的乘法,而不是 mod 运算。

Delphi's PRNG, like almost all programming language RTL PRNGs, is a linear congruential generator.

It's good enough for most small-scale things, but there are things to watch out for. In particular, watch out for low-order bits: the pattern of multiplication and add means that low-order bits are not very random at all. But this generally only applies to large 32-bit values pulled out and then truncated with mod or similar. Using Random(10) to pluck a value between 0 and 9 internally uses a multiplication over the whole 32-bit range rather than a mod operation.

北凤男飞 2024-10-05 09:01:18

alt text

我无法抗拒。

alt text

I couldn't resist.

掩饰不了的爱 2024-10-05 09:01:18

Random 对于您的统计测试是否足够可靠取决于您打算使用它的上下文。

话虽如此,我已经编写了几段需要进行适当统计的Delphi代码,并使用了Random来获取各种空分布、数据伪复制和重采样。到目前为止,我在自己的代码中还没有遇到过任何情况,其中 Random 会产生有偏差或不可靠的结果,或者结果会妨碍其用于预期的统计测试。但适用于我的代码的内容不一定也适用于您的代码。

如果有疑问,您当然可以统计分析调用 Random 的结果(例如在 R、SPSS 等中),并检查结果的分布是否违反您的特定统计测试的分布要求( s)。 [如果你是一名合格的科学家,无论如何这就是你应该做的。]

如果你需要其他 PRNG - 例如 TPMath 库包含一些。 (对于更复杂的事情,还可以选择通过 Delphi 从 R 调用复杂的统计函数。)

Whether Random is sufficiently reliable for your statistical tests will depend on the context in which you intend to use it.

Having said that, I have written several pieces of Delphi code that need to do proper statistics, and have used Random e.g. for obtaining various null distributions, data pseudo-replications and resamplings. So far, I have not come across any case in my own code where Random would have yielded biased or unreliable results, or results which would have precluded its use for the intended statistical test. But what holds for my code does not necessarily have to hold for yours.

If in doubt, you could of course statistically analyse the results of calls to Random (e.g. in R, SPSS, etc.) and examine whether the distribution of results violate the distributional requirements for your particular statistical test(s). [If you're a proper scientist, this is what you should do anyway.]

Should you need other PRNGs - e.g. the TPMath library contains some. (For more involved things, there's also the option of calling elaborate statistical functions from R via Delphi.)

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