Problem with randint function

Asked

Viewed 1,630 times

2

Hello, I’m having a little problem with my code. I try to generate a list of 4 random integer numbers that do not repeat, but...

list_num = []
c = 0
v = random.randint(0, 9)

while c < 4 and v not in list_num:
    list_num.append(v)
    c += 1
    v = random.randint(0, 9) # O problema está aqui

When trying to run the line v = random.randint(0, 9) inside the loop, the content that already existed in the variable v is not replaced by the new content generated by the code. I noticed this when running it in python tutor. Which may be causing the error?

  • 2

    I ran this code in the shell, and it worked smoothly (ex.: [8, 0, 2, 9]). Probably the problem is in python tutor... (there is also a 10 chance that the second value will be identical to the first one however, but that’s not the case) I ran the code twice in pythontutor.com and had 6 in both - showing that it is probably not generating random numbers as it should. The reason for this I do not know.

3 answers

3


If you are referring to the site http://www.pythontutor.com/, The problem is that he’s probably using a fixed seed in his random number generator. My guess is that this is being done so that the results are always deterministic. By chance, the first two calls to random.randint(0,9) returned the same value (6) for the seed used (1 in 10 chance of happening, and it happened!) and so seemed that the value was not overwritten, when in fact it was.

Try assigning a seed to the random, and you should have different results. In my case, I got a list with 4 elements using:

import random
random.seed(10);

list_num = []
c = 0
v = random.randint(0, 9)

while c < 4 and v not in list_num:
    list_num.append(v)
    c += 1
    v = random.randint(0, 9)

You can always, of course, use the clock as a seed, so that the execution is different every time.

And as for the original problem ("generating a list of 4 random integers that do not repeat"), this code needs to be adapted, because it is simply stopping when it finds the first one that repeats... I suggest the following: (caring for: if the number of elements you ask for is greater than the number of elements available, this code will enter an infinite loop)

while c < 4:
    while v in list_num:
        v = random.randint(0, 9)
    list_num.append(v)
    c += 1

I tried it on python tutor without using any seed, and I obtained [6, 0, 4, 8].

2

Save friend, follow random numbers code without repetitions. I hope it helps you

import random
lista = []
while len(lista) < 4:
    x = random.randint(1, 50)
    if x not in lista:
        lista.append(x)
lista.sort()     #Aqui ele vai ordenar em crescente do menor para o maior.
print(lista)

0

The best strategy to draw whole numbers, random and without repetitions is using the method sample library Random. In this way we can use the following code:

from random import sample

list_num = sorted(sample(range(0, 9), 4))
print(list_num)

Note that when we run this code, the method sample will draw 4 whole numbers, random and without repetitions, belonging to the range(0, 9) and then with the help of the Sorted, will order the elements in ascending order and then display them.

OBSERVING: As the function crease() python starts on limite inferior and goes to the limite superio - 1, meaning to say that, for the range(0, 9), values will be drawn between 0 and 9 - 1, that is, values between 0 and 8.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.