Maybe very basic question but is there a random function that gives me the integers eg. 1 or 6.
I don't mean random.randint(1, 6). I want either 1 or 6, no numbers in between.
Maybe very basic question but is there a random function that gives me the integers eg. 1 or 6.
I don't mean random.randint(1, 6). I want either 1 or 6, no numbers in between.
import random
random.choice((1, 6))
This will return a random selection from the tuple you pass in. Since the tuple only contains the numbers 1 and 6, those are the only possible outputs.
You can use this trick:
random.randint(0, 1) * 5 + 1
How it works:
First, you choose a number between 0 or 1 randomly. Second, you multiply it by 5 which will result to either 0 or 5. At last you add 1 which is going to change (0 or 5) to (1 or 6).
Here is a JavaScript version of the expression that randomly gives you either 1 or 6:
1 + Math.floor(Math.random() * 2) * 5