Difference between revisions of "Python:Random Numbers"
(Created page with "== Introduction ==") |
|||
Line 1: | Line 1: | ||
== Introduction == | == 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: | ||
+ | <syntaxhighlight lang=python> | ||
+ | import numpy as np | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === seed === | ||
+ | The [https://numpy.org/doc/stable/reference/random/generated/numpy.random.seed.html 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 [https://numpy.org/doc/stable/reference/random/generated/numpy.random.randint.html np.random.randint(low, high=None, size=None, dtype=int)] command takes anywhere between one and four arguments. | ||
+ | * If only <code>low</code> is given, a single random integer between 0 (inclusive) and <code>low</code> (exclusive) will be returned. In other words, with only one argument, <code>low</code> is really high... | ||
+ | * If both <code>low</code> and <code>high</code> are given, a single random integer between <code>low</code> (inclusive) and <code>high</code> (exclusive) will be returned. | ||
+ | * If a <code>size</code> is not given, a single number will be returned. If a single value is given for <code>size</code>, a 1-D array of that size will be returned; if <code>size</code> is an $$n$$-term tuple, an $$n$-dimensional array is returned with the sizes specified. '''Note:''' without a <code>size</code>, 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 <code>size=1</code> to get an array with one entry versus a single number. | ||
+ | * <code>dtype</code> is the desired data type; by default it is <code>int</code>. 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: | ||
+ | <syntaxhighlight lang=python> | ||
+ | rolls = np.random.randint(1, 7, 5) | ||
+ | </syntaxhighlight> | ||
+ | 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: | ||
+ | <syntaxhighlight lang=python> | ||
+ | rolls = np.random.randint(1, 7, (10, 5)) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === Uniformly Distributed Random Numbers === | ||
+ | The [https://numpy.org/doc/stable/reference/random/generated/numpy.random.uniform.html np.random.uniform(low=0.0, high=1.0, size=None)] command takes anywhere between zero and three arguments. | ||
+ | * A number or numbers between <code>low</code> (inclusive) and <code>high</code> (exclusive, but possible due to roundoff) will be returned. | ||
+ | * If a <code>size</code> is not given, a single number will be returned. If a single value is given for <code>size</code>, a 1-D array of that size will be returned; if <code>size</code> is an $$n$$-term tuple, an $$n$-dimensional array is returned with the sizes specified. '''Note:''' as above, without a <code>size</code>, 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 <code>size=1</code> 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: | ||
+ | <syntaxhighlight lang=python> | ||
+ | vals = np.random.uniform(-2, 2, 4) | ||
+ | </syntaxhighlight> | ||
+ | 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: | ||
+ | <syntaxhighlight lang=python> | ||
+ | vals = np.random.uniform(0, 4, (5, 3)) | ||
+ | </syntaxhighlight> |
Revision as of 22:47, 17 October 2023
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) andlow
(exclusive) will be returned. In other words, with only one argument,low
is really high... - If both
low
andhigh
are given, a single random integer betweenlow
(inclusive) andhigh
(exclusive) will be returned. - If a
size
is not given, a single number will be returned. If a single value is given forsize
, a 1-D array of that size will be returned; ifsize
is an $$n$$-term tuple, an $$n$-dimensional array is returned with the sizes specified. '''Note:''' without a <code>size</code>, 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 <code>size=1</code> to get an array with one entry versus a single number. * <code>dtype</code> is the desired data type; by default it is <code>int</code>. There may be some applications that require other data types, such as np.int32. ===='"`UNIQ--h-4--QINU`"' Examples ==== Let's say you want to roll 5 6-sided dice. You could use: '"`UNIQ--syntaxhighlight-00000002-QINU`"' 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: '"`UNIQ--syntaxhighlight-00000004-QINU`"' ==='"`UNIQ--h-5--QINU`"' Uniformly Distributed Random Numbers === The [https://numpy.org/doc/stable/reference/random/generated/numpy.random.uniform.html np.random.uniform(low=0.0, high=1.0, size=None)] command takes anywhere between zero and three arguments. * A number or numbers between <code>low</code> (inclusive) and <code>high</code> (exclusive, but possible due to roundoff) will be returned. * If a <code>size</code> is not given, a single number will be returned. If a single value is given for <code>size</code>, a 1-D array of that size will be returned; if <code>size</code> is an $$n$$-term tuple, an $$n$-dimensional array is returned with the sizes specified. Note: as above, without asize
, 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 addsize=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))