随机数random_随机数

随机数random

If computers only do what we tell them, how do they create random numbers?

如果计算机仅按照我们告诉他们的方式工作,它们如何创建随机数?

Random numbers are all around us, particuarly when we look at computers. Our “auto-generated” passwords, the amount of coins you win for logging in daily to your favorite game, and, of course, the =RAND() Excel function — all random. So where do these random numbers come from? Is there some magical random place within your computer?

随机数无处不在,尤其是当我们看着计算机时。 我们的“自动生成”密码,每天登录您​​喜欢的游戏所赢得的硬币数量,当然还有= RAND()Excel函数-都是随机的。 那么这些随机数从何而来呢? 您的计算机中是否有一些神奇的随机位置?

Like all things in computer (quantum computers excluded), things just don’t happen on their own. Computers do what they’re programmed to do. The same applies to random numbers. Not to burst your bubble, but those “random” numbers aren’t actually random, as we’ll see. In fact they’re made with simple algorithms you can quickly create yourself.

像计算机中的所有事物(不包括量子计算机)一样,事物并不是独立发生的。 计算机可以按照编程的方式进行操作。 随机数也是如此。 不会破灭您的泡沫,但是正如我们将看到的那样,那些“随机”数字实际上并不是随机的。 实际上,它们是用简单的算法制成的,您可以快速创建自己。

随机数的起源 (Origins of Random Numbers)

To create random numbers, we typically use a Random Number Generator (RNG) (but of course we do…). The first RNG was devised by John von Neumann in the 1940’s. Many current methods still piggyback off of his initial work. Von Neumann suspected that he could start with any number he could think of (known as a seed), square it, and slice out the middle numbers. So if we started with 675248, we’d then square it to get 455 959861 504, we’d then slice out the middle numbers (959861) to arrive at our “random number” . From there, we could then use 959861 as our seed to repeat the process, getting 51 as our second “random” number.

要创建随机数,我们通常使用随机数生成器(RNG)(但我们当然会…)。 第一个RNG由John von Neumann在1940年代设计。 当前的许多方法仍然背负着他的最初工作。 冯·诺依曼(Von Neumann)怀疑他可以以他能想到的任何数字(称为种子)开头,平方并除掉中间的数字。 因此,如果我们从675248开始,我们将对它求平方以得到455 959861 504,然后将中间数(959861)切掉以得出“随机数”。 然后,从那里开始,我们可以使用959861作为种子来重复该过程,获得51作为我们的第二个“随机”数字。

As you can see, there’s really nothing random about this method. It’s computed using a simple equation, yet it does produce values that appear random to us. Because of these two properties, we’ll call these number pseudo-random numbers (PRN). Today’s algorithms commonly utilize the same foundation, but of course have advanced significantly. Most continue to start with an initial number (seed), perform a computation, and reiterate that computation with the last result. Von Neumann’s work isn’t used today because people noticed the “random numbers” quickly start to repeat themselves in this cycle. Today’s algorithms are commonly optimized to repeat only after billions or trillions of runs.

如您所见,此方法实际上没有任何随机性。 它使用一个简单的方程式进行计算,但确实会产生随机出现的值。 由于这两个属性,我们将这些数字称为伪随机数(PRN)。 当今的算法通常使用相同的基础,但是当然已经有了很大的进步。 大多数继续从初始编号(种子)开始,执行计算,并以最后结果重复该计算。 冯·诺依曼(Von Neumann)的工作今天没有使用,因为人们注意到“随机数”在这个周期中Swift开始重复。 通常,对当今的算法进行优化,使其仅在数十亿或数万亿次运行后才重复。

创建自己的随机数生成器 (Create Your Own Random Number Generator)

A simple, but pretty useful random number generator is called the Linear Congruent Generator (LCG) — and yes, it sounds much worse than it really is. Like Von Neumann, you start with a seed. We then multiply the seed by a special prime number, and the perform a modulo with it and another prime number. (These prime numbers are selected to ensure they cycle repeats only after very long runs). We then plug our random number back into the system as the new seed.

一个简单但非常有用的随机数生成器称为线性同余生成器(LCG),是的,听起来比实际情况要糟糕得多。 像冯·诺依曼(Von Neumann)一样,您从种子开始。 然后,我们将种子乘以一个特殊的质数,并对它和另一个质数执行取模运算。 (选择这些素数以确保仅在很长一段时间后才循环重复)。 然后,我们将随机数作为新种子插入到系统中。

#Simple Linear Congruential Generator (LCG) import numpy as np def generator(seed, n,):
#create an empty list to store our n random numbers
array_of_rn = np.empty(n) for i,_ in enumerate(array_of_rn):
random_number = np.mod((58598 * seed), 233280)
#save the random number
array_of_rn[i] = random_number
#reset the seed to the last random number
seed = random_number return array_of_rn generator(1438786,10) ## array([ 23948., 125704., 186992., 195616., 27008., 43264., 130112., 12736., 41408., 80704.])

错误的随机数生成器会发生什么? (What Happens with Bad Random Number Generators?)

Remember those “special prime numbers” we talked about in the last section? Yes, those really are needed. Let’s see why. Below is a plot of 100 randomly generated numbers using our algorithm above (I generated random x’s and random y’s and plotted).

还记得我们在上一节中提到的那些“特殊质数”吗? 是的,确实需要这些。 让我们看看为什么。 下面是使用我们上面的算法绘制的100个随机生成的数字的图(我生成了随机的x和y,然后作图)。

Image for post

As you can see, everything really does look random. Now, let’s use the exact same algorithm, same seeds, same everything, except change those special prime numbers to 2 and 8. Again, we’ll generate 100 points using two lists of random numbers.

如您所见,所有内容确实看起来都是随机的。 现在,让我们使用完全相同的算法,相同的种子,所有内容,除了将那些特殊的质数更改为2和8。同样,我们将使用两个随机数列表生成100点。

Image for post

No, it’s not a mistake. You only see 3 points. Why? Because without those special primes, our algorithm continually repeats itself. In this case, each cycle is 3 points and the same 3 “random numbers” appear over and over again.

不,这不是错误。 您只会看到3分。 为什么? 因为没有那些特殊的素数,我们的算法会不断重复自身。 在这种情况下,每个周期为3点,并且相同的3个“随机数”反复出现。

外卖 (Takeaways)

Hopefully you’ve learned a little about how those random-numbers you see are made. If you look close, you’ll start to see them everywhere — especially with all of those new two-factor authentication apps. Of course today’s top-of-the-line RNGs are much more complex than the simple ones we’ve covered — and will likely get even more complex with the rise of quantum computing. But for now, the underlying mechanics are the same. They’re not truly random, but they’re the best we can do for now and they generally do the trick.

希望您已经学到了一些关于如何看到这些随机数的知识。 如果您仔细观察,您将开始在各处看到它们-尤其是在所有这些新的两因素身份验证应用程序中。 当然,当今最先进的RNG比我们已经介绍的简单RNG复杂得多,并且随着量子计算的兴起,它可能会变得更加复杂。 但就目前而言,底层机制是相同的。 它们并不是真正随机的,但它们是我们目前能做的最好的事情,它们通常可以解决问题。

Originally published at http://lowhangingfruitanalytics.com on August 25, 2020.

最初于 2020年8月25日 发布在 http://lowhangingfruitanalytics.com 上。

翻译自: https://medium.com/the-innovation/random-numbers-arent-so-random-bf786ceef1c4

随机数random

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值