简体   繁体   English

如何在 Python 3 中生成 0 到无穷大之间的 N 个随机数

[英]How to Generate N random numbers in Python 3 between 0 to infinity

How do I generate n random numbers in python 3?如何在python 3中生成n个随机数? n is a to be determined variable. n是待定变量。 preferably natural numbers (integers > 0), All answers I've found take random integers from a range, however I don't want to generate numbers from a range.最好是自然数(整数> 0),我发现的所有答案都从一个范围中随机取整数,但是我不想从一个范围中生成数字。 (unless the range is 0 to infinity) (除非范围是 0 到无穷大)

To paraphrase Wittgenstein, the limits of your machine is the limits of your language.套用维特根斯坦的话,你机器的极限就是你语言的极限。 ie There is no such thing as infinity in computers/computation world.即在计算机/计算世界中没有无穷大这样的东西。 You can get the largest positive integer supported by your machine using sys.maxsize ( sys.maxint in python 2) and pass it to random.randint function:您可以使用sys.maxsize (python 2 中的sys.maxint )获取您的机器支持的最大正整数并将其传递给random.randint函数:

>>> import sys
>>> sys.maxsize
9223372036854775807
>>> random.randint(0,sys.maxsize)
7512061515276834201

And for generating multiple random numbers you can use a list comprehension like following:为了生成多个随机数,您可以使用如下列表理解:

>>> N = 10
>>> [random.randint(0,sys.maxsize) for _ in range(N)]
[3275729488497352533, 7487884953907275260, 36555221619119354, 1813061054215861082, 619640952660975257, 9041692448390670491, 5863449945569266108, 8061742194513906273, 1435436865777681895, 8761466112930659544]

For more info about the difference of sys.maxint and sys.maxsize in python 2.X and 3.X :有关python 2.X 和 3.Xsys.maxintsys.maxsize差异的更多信息:

The sys.maxint constant was removed, since there is no longer a limit to the value of integers.删除了sys.maxint常量,因为对整数的值不再有限制。 However, sys.maxsize can be used as an integer larger than any practical list or string index.但是, sys.maxsize可以用作比任何实际列表或字符串索引都大的整数。 It conforms to the implementation's “natural” integer size and is typically the same as sys.maxint in previous releases on the same platform (assuming the same build options).它符合实现的“自然”整数大小,并且通常与同一平台上以前版本中的sys.maxint相同(假设相同的构建选项)。

I think you probably need to rethink what it is you're trying to do with the random number you want.我认为你可能需要重新考虑你想要用你想要的随机数做什么。 In particular, what distribution are you sampling the number from?特别是,您从哪个分布中抽样? If you want your random numbers uniformly distributed (equal probability of each number being chosen), you can't: you'd need an infinite amount of memory (or time, or both).如果您希望随机数均匀分布(每个数字被选择的概率相等),则不能:您需要无限量的内存(或时间,或两者)。

Of course, if you allow for non-uniform distributions, here are some random numbers between 1 and (roughly) the largest float my system allows, but there are gaps due to the way that such numbers are represented.当然,如果您允许非均匀分布,这里有一些介于 1 和(大致)我的系统允许的最大float之间的随机数,但由于这些数字的表示方式存在差距。 And you may feel that the probability of "large" numbers being selected falls away rather quicker than you'd like...而且您可能会觉得“大”数字被选中的可能性比您希望的要快得多……

In [254]: [int(1./random.random()) for i in range(10)]
Out[254]: [1, 1, 2, 1, 1, 117, 1, 3, 2, 6]

Here we have a limitation of memory, so we can get the random numbers to the maximum a system can reach.这里我们有内存限制,因此我们可以将随机数设置为系统可以达到的最大值。 Just place the n digit numbers you want in the condition and you can get the desired result .As an example I tried for 6 digit random numbers.One can try as per the requirements.只需在条件中放置您想要的n位数字,您就可以获得所需的结果。例如,我尝试了6位随机数。可以根据要求尝试。 Hope this solves your question to an extent.希望这能在一定程度上解决您的问题。

import sys导入系统

from random import *从随机导入 *

for i in range(sys.maxsize): print(randint(0,9),randint(0,9),randint(0,9),randint(0,9),randint(0,9),randint(0,9),sep='')对于 i in range(sys.maxsize): print(randint(0,9),randint(0,9),randint(0,9),randint(0,9),randint(0,9),randint(0) ,9),sep='')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM