Python Tip #315 (of 365): Don't reach for random.random. Need a random integer? ...

@treyhunner@mastodon.social

Python Tip #315 (of 365):

Don't reach for random.random.

Need a random integer? Don't multiply a random float and convert the result. The randint function does that more directly.

Do this:
random.randint(1, 6)

Not this:
int(random.random() * 6) + 1

In JavaScript, Math.random is the random number generation utility. Need a random integer? Use Math.random. Need a random item from an array? Use Math.random.

More on this:
pym.dev/random-numbers/

๐Ÿงต (1/3)

#Python #DailyPythonTip