随机数如何生成?

发布于 2024-11-27 12:23:01 字数 153 浏览 1 评论 0原文

我想知道随机数函数是如何工作的。我的意思是使用服务器时间还是使用其他哪些方法来生成随机数?它们真的是随机数还是倾向于某种模式?让我们用Python来说:

import random
number = random.randint(1,10)

I was wondering how the random number functions work. I mean is the server time used or which other methods are used to generate random numbers? Are they really random numbers or do they lean to a certain pattern? Let's say in python:

import random
number = random.randint(1,10)

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

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

发布评论

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

评论(4

毅然前行 2024-12-04 12:23:01

随机数生成器(当然)因不同平台而异,但一般来说,它们只是“伪随机”数。也就是说,“随机”数字是由一种算法生成的,该算法被选择来提供相当均匀的数字分布,并且具有类似于人们所期望的真正随机性的统计分布。这些随机数生成器通常采用“种子”值,用于启动“序列”;通常,相同的“种子”值将返回相同的“随机”数字(表明它实际上不是“随机”)。

然而,通过使用快速变化的数字(例如来自机器的时间(以滴答为单位)或其他变化的种子值)作为“随机”数字函数的种子,可以获得合理的伪随机结果。然而,这并没有改变这样一个事实:这些“随机”数字并不是真正随机的;而是随机的。然而,对于大多数用途,它们可以被认为“足够好”。

作为附录,需要注意的是:有一些基于硬件的实际随机数生成器,可以购买和使用实际上是随机的。这些通常取决于对变化量的测量,例如探测器接收到的光子数,并且有偏差,以便它们返回真正的随机值。然而,这些相对较少。

Random number generators vary (of course) by different platform, but in general, they're only "pseudo-random" numbers. That is, the "random" numbers are generated by an algorithm that is chosen to provide a distribution of numbers that's reasonably even and with a statistical distribution similar to what one would expect of true randomness. These random number generators typically take a "seed" value, which is used to initiate the "sequence"; usually, the same "seed" value will return the same "random" number (indicating that it's clearly not actually "random").

One can obtain reasonable pseudorandom results, however, by seeding the "random" number function with a rapidly changing number, such as the time (in ticks) from the machine, or other varying seed values. That doesn't change the fact, however, that these "random" numbers aren't really random; however, for most purposes, they can be considered "good enough".

One note as an addendum: there are actual random number generators that are hardware based that can be purchased and used that actually are random. These typically depend on the measurement of a varying quantity, such as the number of photons received by a detector, and biased such that they return truly random values. These are relatively rare, however.

泛泛之交 2024-12-04 12:23:01

是的,当数字不可预测并不重要时,时间通常用于为随机数生成器提供种子。例如,如果您在幻灯片中显示随机图像,那么该时间是一个很好的使用值,这样下次运行幻灯片时图像的顺序就不会相同。

然而,由于每个人都知道时间的准确性,因此对于加密目的而言,这将是一个可怕的种子。 Netscape 曾经使用过此方法,并且它被证明容易受到攻击。如今,安全随机数是使用鼠标移动和麦克风输入等设备收集的熵生成的。 “无头”网络设备使用其网络流量的特征作为或多或少不可预测的熵源。对于真正特殊的应用,有时会使用硬件随机源,例如相机和盖革计数器。在 UNIX 系统上,您可以从 /dev/random 获取安全随机数,如果没有“足够的熵”(通过计数器估计)来保证安全随机性,它将阻塞。

Yes, time is typically used to seed a random number generator when it's not important that the numbers be unpredictable. For example, if you are displaying random images in a slideshow then the time is a good value to use so that the sequence of images isn't the same the next time you run the slideshow.

However, since the time is known by everyone to a high degree of accuracy, this would be a terrible seed for crypto purposes. Netscape used to use this method and it was shown to be vulnerable to attack. Nowadays secure random numbers are generated using entropy gathered by devices like mouse movement and microphone input. "Headless" network devices use characteristics of its network traffic as a more-or-less unpredictable entropy source. For really special applications sometimes hardware randomness sources are used like cameras and Geiger counters. On unix systems you can get secure random numbers from /dev/random and it'll block if there's not "enough entropy" (estimated through a counter) to guarantee secure randomness.

短暂陪伴 2024-12-04 12:23:01

它的伪随机数生成器,确切的工作取决于实现,但我假设它是 Mersen-Twister 的某种 c 实现: http://docs.python.org/library/random.html(第三段)

哦,确切的函数randint是建立在基础random 功能。 Random 返回范围 (0,1] 中的实数,而 randint(a,b) 则返回范围 [a,b] 中的整数,并且可以实现为 lambda a,b: int( a + random.random()*(b+1-a))

Its pseudo random number generator, exact working depends on implementation, but I assume it's some kind of c implementation of Mersen-Twister: http://docs.python.org/library/random.html (Third paragraph)

Oh, and exact function randint is built upon base random function. Random returns real number from range (0,1], and randint(a,b) returns integer from range [a,b] and can be implemented as lambda a,b: int(a + random.random()*(b+1-a))

薄荷→糖丶微凉 2024-12-04 12:23:01

根据您的背景,您可能会喜欢数字食谱。我是一名物理学家
我真的很喜欢这本书(尽管数学家偶尔
写一些关于它的坏话,它对很多主题提供了很好的概述)。

请参阅第 7 章,了解随机数的精彩介绍。

Depending on your background you might like Numerical Recipes. I am a physicist
and I really like this book (even though mathematicians occasionally
write bad things about it, it gives nice overviews on a lot of topics).

See chapter 7 for a nice introduction into random numbers.

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