Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python random number excluding one variable

Is it possible to create a variable with a random number except for one number that is stored in a variable?

For example:

import random
x = raw_input("Number: ")
y = random.randint(1,6)

So the variable x could never be y

like image 212
Gewoo Avatar asked Jun 21 '26 04:06

Gewoo


1 Answers

I would suggest you to use random.choice form the list of numbers sans your number of choice

import random
x = raw_input("Number: ")
y = random.choice(range(1, x) + range(x+1, 6))
like image 87
Abhijit Avatar answered Jun 22 '26 19:06

Abhijit