jeudi 16 janvier 2020

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



Aucun commentaire:

Enregistrer un commentaire