1

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.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Withi Weis
  • 53
  • 5
  • Choose a random value of 0 or 1 and select the number you want based on the two possible outcomes. – paddy Dec 14 '21 at 21:41
  • 1
    Does this answer your question? [How can I randomly select an item from a list?](https://stackoverflow.com/questions/306400/how-can-i-randomly-select-an-item-from-a-list) – DanielTuzes Dec 15 '21 at 11:30

3 Answers3

1
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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

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).

Mustafa
  • 197
  • 1
  • 7
0

Here is a JavaScript version of the expression that randomly gives you either 1 or 6:

1 + Math.floor(Math.random() * 2) * 5
Danny Raufeisen
  • 953
  • 8
  • 21