There are two relevant modules. random is for basic general purpose randomization; numpy.random is for more advanced randomization for scientific computing, statistics, machine learning, etc.

  • Generate random integer:

      random.randint(a,b) # Returns a random integer in the range [a,b],
                          # including both end points.
    
  • Basic random choice:

     random.choice(seq) # Choose a random element from a non-empty sequence.
    
  • Basic sampling:

      random.sample(population, k) # return a new list containing k unique elements
                                   # chosen from the population sequence or set.
    
  • Sampling with counts:

      sample(['red', 'blue'], counts=[4, 2], k=5) # equivalent to 
      sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5)
    
  • Mutating shuffle:

      random.shuffle(x) # Shuffle list x in place, and return None.
    
  • Non-mutating shuffle:

      random.sample(x, len(x)) # Return a new shuffled list from the elements of x.
    
  • Random float in \([0.0, 1.0)\):

      random.random() # Return the next random floating point number in the range [0.0, 1.0).