Ask coding questions

← Back to all posts
About the randint function
TomColes (0)

I was wondering if there was a way to eliminate options from a pool of random numbers. For instance, if I had variable = randint(1,10) but I didnt want it to pick say the number nine, if theres a way of skipping that number.

Comments
hotnewtop
RYANTADIPARTHI (5999)

maybe write an if statement to see if 9 comes up, and remove it form list. list.remove

19ecal (228)

You can do this a couple of ways
1, You can use random.choice()
It returns a randomly chosen element from a list

import random
arr=[1, 3, 4, 6, 7, 10]
print(random.choice(arr))

2, You can just reject it

disallowed=[9, 3, 6]
num=3
while num in disallowed:
  num=random.randint(1, 10)