Python:Random Numbers

From PrattWiki
Jump to navigation Jump to search

Introduction

This page is going to cover just the very basics of using random numbers in Python! This page will be using the NumPy module's legacy random number commands. There are newer commands using generators. This is not the place to learn about that...

Basic Commands

All of the code below assumes you have already run:

import numpy as np

seed

The np.random.seed(seed=None) command will accept an integer as an argument. That argument will reseed the random number generator. The value in this is that you can then generate the same set of random numbers multiple times. Among other things, this is useful in autograding assignments that rely on random numbers.

Random Integers

The np.random.randint(low, high=None, size=None, dtype=int) command takes anywhere between one and four arguments.

  • If only low is given, a single random integer between 0 (inclusive) and low (exclusive) will be returned. In other words, with only one argument, low is really high...
  • If both low and high are given, a single random integer between low (inclusive) and high (exclusive) will be returned.
  • If a size is not given, a single number will be returned. If a single value is given for size, a 1-D array of that size will be returned; if size is an $$n$$-term tuple, an $$n$$-dimensional array is returned with the sizes specified. Note: without a size, a number is returned, not an array! If your program wants to be able to index the result, even if there is only one number, you need to add size=1 to get an array with one entry versus a single number.
  • dtype is the desired data type; by default it is int. There may be some applications that require other data types, such as np.int32.

Examples

Let's say you want to roll 5 6-sided dice. You could use:

rolls = np.random.randint(1, 7, 5)

to get 5 integers between 1 and 6 (remember, the high value is exclusive!).

If you wanted to get 10 rows of 5 columns of 6-sided dice rolls, you could use:

rolls = np.random.randint(1, 7, (10, 5))

Uniformly Distributed Random Numbers

The np.random.uniform(low=0.0, high=1.0, size=None) command takes anywhere between zero and three arguments.

  • A number or numbers between low (inclusive) and high (exclusive, but possible due to roundoff) will be returned.
  • If a size is not given, a single number will be returned. If a single value is given for size, a 1-D array of that size will be returned; if size is an $$n$$-term tuple, an $$n$$-dimensional array is returned with the sizes specified. Note: as above, without a size, a number is returned, not an array! If your program wants to be able to index the result, even if there is only one number, you need to add size=1 to get an array with one entry versus a single number.

Examples

Let's say you want to get 4 numbers between -2 and 2. You could use:

vals = np.random.uniform(-2, 2, 4)

You will most likely not get a 2 unless the random number generator has a roundoff issue.

If you wanted to get 5 rows of 3 columns of numbers between 0 and 4, you could use:

vals = np.random.uniform(0, 4, (5, 3))