Generate random numbers without repeating the previously generated number

Asked

Viewed 2,148 times

1

I am trying to make a program that simulates the game "Door of Hope" of Silvio Santos in Pyton and I need the program to generate a random number, excluding the number previously generated. I did it that way:

premio = rd.randint(1,3)

p = int(premio)

print(p)

w = rd.sample(range(1,3), p)

print(w)

But a warning appears saying that the sample is larger than the population. I do not know how else to do!

p.s.: for those who want to help the program as a whole, here is the question:

Make a program that simulates the behavior of Silvio Santos. Your program must:

  1. randomly choose one of the three doors and place the prize behind it (do not tell the user where the prize is);
  2. ask for the user chooses one of the three ports; open a port that does not have the prize and that the user has not chosen (if there is more than one choice for the port to be open, your program should open any of them with equal probability);
  3. show user which port was opened;
  4. ask the user if he wants to keep his initial choice or if he wants to change port;
  5. show the user whether or not he has won the prize.
  • 1

    Make a Fisher-Yates: https://answall.com/search?q=fisher+yates

  • This problem must be occurring because 'p' must have received value 3, but range(1,3) generates a list of 2 numbers: [1, 2]. The correct would be range(1.4)

2 answers

1

First make a list of doors. Let’s assume that we are working with 10 doors.

*l, = range(1,11)

will generate:

>>l
[1,2,3,4,5,6,7,8,9,10]

Now let’s delete a random number from the list. Just shuffle the items and remove the last one. It will be the prize.

import random
random.shuffle(l)
premio = l.pop()

Ask the user to choose a port.

user = input('Escolha uma porta de um a dez')

Remove the port the user chose from the list.

try:
    l.remove(int(user))
except:
    pass

We use Try/except to avoid a Valueerror if he chooses the door with the prize ( that is no longer on the list and therefore can not be removed ).

We have already removed from the list the door with the prize and the port that the user chose. Just shuffle again and take out the last: this port does not have the prize, nor was it chosen by the user.

random.shuffle(l)
print('A porta ' + str(l.pop()) + ' foi aberta')

The rest of the program is trivial.

Just compare the new chosen port, int(new) obtained from new = input(), or the maintained port, int(user) with the port stored in the variable press.

-1

Just generate a number and add it to an array, then when generating a new number do a foreach or something like that to go through all positions of the array checking if any is equal to the new generated number. If it is, it generates a new number and the increment variable to 0 if there is no equal adds in the last position filled + 1

  • 2

    John, we often fail to realize the degree of learning of people, just describing logic is not a good answer. Try to answer through practical examples so that anyone (or almost) can understand the answer.

Browser other questions tagged

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