简体   繁体   English

随机数列表生成器未按预期工作(Python)

[英]Random number list generator not working as expected (Python)

I made a program that is meant to print a list of numbers chosen randomly from an array of numbers, without repeating any number.我制作了一个程序,旨在打印从数字数组中随机选择的数字列表,而不重复任何数字。

For example I expected the following:例如,我期望以下内容:

number_list(180, 222, 5)
219, 180, 185, 191, 197, 

But the results my program gave me are all kind of similar and the numbers generated are always near to the extremes value of the array (180 and 222).但是我的程序给我的结果都差不多,生成的数字总是接近数组的极值(180 和 222)。 For example:例如:

219, 180, 182, 181, 184, 
221, 181, 180, 183, 184,
219, 221, 222, 180, 181,
222, 219, 181, 180, 182, 

At this point I think that there must be some problem with the program I've written and it's not a problem caused by the function random.randomint().此时我认为我写的程序一定有问题,不是函数random.randomint()引起的问题。

The code I've used is the following:我使用的代码如下:

from random import randint

def number_list(start, end, length):
    tot_list = []
    for i in range(start, end+1):
        tot_list.append(i)
    list_len = len(tot_list)
    while(length > 0):
        index = start - randint(start-1, start + length-1)
        length = length -1
        number = tot_list[index]
        tot_list.remove(number)
        print(str(number) + ", ")

number_list(180, 222, 5)

You very specifically do not take the number randomly form the remaining list:你很特别采取随机数生成剩余列表:

    index = start - randint(start-1, start + length-1)

You're confusing values with indices here and below.您在此处和下面将值与索引混淆了。 In your given example, this looks for a random number with limits 179 - 184, and subtracts that from the starting value to get an index into your list.在您给定的示例中,这将查找限制为 179 - 184 的随机数,并将其从起始值中减去以将索引添加到您的列表中。 This gives you numbers in the range -start+1 through 1 ... or -4 through 1 in this example.在此示例中,这将为您提供 -start+1 到 1 ... 或 -4 到 1 范围内的数字。 Those are at the ends of your pick-list.这些都在您的选择列表的末尾。

I easily found the problem with basic debugging:我很容易发现基本调试的问题:

    print("TRACE", start, length, index, number, tot_list)

See this lovelydebug blog for help.请参阅这个可爱的调试博客以获取帮助。 Insert useful output statements to trace the control and data flow.插入有用的输出语句来跟踪控制和数据流。 Remove inapplicable code;删除不适用的代码; reduce working code to a hard-coded result.将工作代码简化为硬编码结果。 As the posting guidelines say, "make it easy for others to help you."正如发帖指南所说,“让别人更容易帮助你”。


Also, note that random already has a function to do this: sample .另外,请注意random已经有一个功能可以做到这一点: sample

The main issue with your current logic lies in picking the index.您当前逻辑的主要问题在于选择索引。 Actually, your statement:其实你的说法是:

index = start - randint(start-1, start + length-1)

Would make a lot of sense if length was the actual length of tot_list .如果lengthtot_list的实际长度, tot_list But it isn't, as length in your code reflects how many random numbers you want to return (5 in this case).但事实并非如此,因为代码中的length反映了您想要返回的随机数的数量(在本例中为 5)。

Therefore, in the above line of code, consider replacing length by the actual length of the list, namely end - start + 1 :因此,在上面的代码行中,考虑将length替换为列表的实际长度,即end - start + 1

index = start - randint(start-1, start + end - start) # -1 + 1 cancel out

Side note: Seriously though, why re-inventing the wheel when you can use:旁注:说真的,当你可以使用时,为什么要重新发明轮子:

random.choices(range(180, 225), k=5)

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

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