简体   繁体   English

Python随机函数似乎总是生成与最大数字长度相同的数字

[英]Python random function seems to always generate numbers of the same length as the max number

I'm new to Python.我是 Python 的新手。 Trying to generate a random number of random numbers and write it to file.尝试生成随机数的随机数并将其写入文件。 I want the default order to be ascending, so in my loop I each time add the "number" chosen randomly in the previous iteration as my minimum value for the next random number (hope that makes sense):我希望默认顺序是升序的,所以在我的循环中,我每次都添加在上一次迭代中随机选择的“数字”作为下一个随机数的最小值(希望有意义):

    # Create random number of random integers and write to file 1:
number = 0  # used later
records = random.randrange(1,100)  # randomly decide how many integers to create (up to 100).
for index in range(1,records):
    # randomly create integer between last number created (to ensure correct sort) and 1 million.
    number = random.randrange(number,100000)
    # write it to file:
    f1.write(str(number) + "\n")
    print(number)

The weird thing is that my output seems to almost always be random numbers but aside from maybe the first 2 or 3 it will immediately jump to a 5 digit number, and then within 4 or 5 iterations it hits and then just repeats the max number (99999) - the following is a common output:奇怪的是,我的输出似乎几乎总是随机数,但除了前 2 或 3 位之外,它会立即跳转到 5 位数字,然后在 4 或 5 次迭代中命中,然后重复最大数字( 99999) - 以下是常见的输出:

50518 84384 87787 91203 91286 92638 93266 97371 99986 99993 99998 99998 99998 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 50518 84384 87787 91203 91286 92638 93266 97371 99986 99993 99998 99998 99998 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999

Why does the max number get hit so easily within a random range?为什么在随机范围内可以如此轻松地达到最大数量?

Your issue is you reduce the amount of numbers that can be generated from each time as you set the minimum value to the last number generated, this increase the likely hood of generating higher numbers as the minimum keeps being raised.您的问题是,当您将最小值设置为最后一个生成的数字时,您会减少每次可以生成的数字数量,这会增加生成更高数字的可能性,因为最小值不断提高。

Instead like others have suggested you should use the random modules sample method which states:相反,就像其他人建议的那样,您应该使用 random modules sample方法,该方法指出:

Return ak length list of unique elements chosen from the population sequence or set.返回从种群序列或集合中选择的唯一元素的 ak 长度列表。 Used for random sampling without replacement.用于不放回的随机抽样。

import random
number = 0  # used later
records = random.randrange(1,100)  # randomly decide how many integers to create (up to 100).
numbers = []
for index in range(1,records):
    print(f'generating numbers between {number} and 100000' )
    number = random.randrange(number,100000)
    numbers.append(number)
    #print(number)
print(numbers)
print(sorted(random.sample(range(100000), records)))

OUTPUT输出

generating numbers between 0 and 100000
generating numbers between 11137 and 100000
generating numbers between 54933 and 100000
generating numbers between 77429 and 100000
generating numbers between 98305 and 100000
generating numbers between 99167 and 100000
generating numbers between 99825 and 100000
generating numbers between 99842 and 100000
generating numbers between 99949 and 100000
generating numbers between 99957 and 100000
generating numbers between 99962 and 100000
generating numbers between 99969 and 100000
[11137, 54933, 77429, 98305, 99167, 99825, 99842, 99949, 99957, 99962, 99969, 99989]
[831, 7001, 18454, 21526, 25156, 29657, 45299, 46796, 61326, 67692, 86185, 90140, 93899]

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

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