I am just wondering how do I make python generate random numbers other than a particular number? For instance, I want it to generate any number from 1 to 5 except 3, so the output would be 1, 2, 4, 5
and 3
will not be counted in the list. What can I do to achieve that?
An example would be like this:
There are five computerized players (Player 0 to 4) in a game.
Player 1 randomly selects one other player (except itself) and Player 2 to 4 will do the same thing.
So the output will be something like:
Player 1, who do you want to play with?
Player 1 chooses Player 2
Best Answer
Use
random.choice
on a list, but first remove that particular number from the list:Or define a function:
Update:
This returns all numbers other than a particular number from the list: