# Tutorial by Examples

## Random and sequences: shuffle, choice and sample

import random shuffle() You can use random.shuffle() to mix up/randomize the items in a mutable and indexable sequence. For example a list: laughs = [&quot;Hi&quot;, &quot;Ho&quot;, &quot;He&quot;] random.shuffle(laughs) # Shuffles in-place! Don't do: laughs = random.shuffle(laughs) p...

## Creating random integers and floats: randint, randrange, random, and uniform

import random randint() Returns a random integer between x and y (inclusive): random.randint(x, y) For example getting a random number between 1 and 8: random.randint(1, 8) # Out: 8 randrange() random.randrange has the same syntax as range and unlike random.randint, the last value is no...

## Reproducible random numbers: Seed and State

Setting a specific Seed will create a fixed random-number series: random.seed(5) # Create a fixed state print(random.randrange(0, 10)) # Get a random integer between 0 and 9 # Out: 9 print(random.randrange(0, 10)) # Out: 4 Resetting the seed will create the same &qu...

## Create cryptographically secure random numbers

By default the Python random module use the Mersenne Twister PRNG to generate random numbers, which, although suitable in domains like simulations, fails to meet security requirements in more demanding environments. In order to create a cryptographically secure pseudorandom number, one can use Syst...

## Creating a random user password

In order to create a random user password we can use the symbols provided in the string module. Specifically punctuation for punctuation symbols, ascii_letters for letters and digits for digits: from string import punctuation, ascii_letters, digits We can then combine all these symbols in a name...

## Random Binary Decision

import random probability = 0.3 if random.random() &lt; probability: print(&quot;Decision with probability 0.3&quot;) else: print(&quot;Decision with probability 0.7&quot;)

Page 1 of 1