1

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). 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().

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)
4
  • 6
    Why not return random.sample(range(start, end + 1), length)? Commented Jan 16, 2020 at 22:47
  • 1
    As to why your current logic doesn't work, it only generates index values around 0 (for those values it's 180 - randint(179, 184), effectively randint(-4, 1) - it'll always be randint(1 - length, 1)); try a debugger or pythontutor.com. Commented Jan 16, 2020 at 22:53
  • How many times did you try it? 20 may not be high enough to start making conclusions. Commented Jan 16, 2020 at 22:54
  • 2
    @EdekiOkoh the logic is definitely broken. Commented Jan 16, 2020 at 22:55

2 Answers 2

3

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. This gives you numbers in the range -start+1 through 1 ... or -4 through 1 in this example. 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 lovely debug 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.

Sign up to request clarification or add additional context in comments.

Comments

1

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. But it isn't, as length in your code reflects how many random numbers you want to return (5 in this case).

Therefore, in the above line of code, consider replacing length by the actual length of the list, namely 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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.