Skip to content

Commit 61ed422

Browse files
Add code examples for NumPy RNG tutorial (#390)
* Add code examples for NumPy RNG tutorial * Final QA --------- Co-authored-by: Geir Arne Hjelle <[email protected]>
1 parent ac82180 commit 61ed422

20 files changed

+237
-0
lines changed

numpy-rng/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# NumPy Random Number Generation
2+
3+
This folder contains supplementary code for the Real Python tutorial on [NumPy's pseudo-random number generator](https://realpython.com/numpy-random-number-generator/). You can copy the code examples, or continue your learning by experimenting more with them.
4+
5+
## Setup
6+
7+
Create and activate a virtual environment, then install the requirements:
8+
9+
```bash
10+
$ python -m venv venv
11+
$ source venv/bin/activate
12+
(venv) $ python -m pip install -r requirements.txt -c constraints.txt
13+
```
14+
15+
For most of the examples, you only need a version of NumPy>=1.17 installed, but you'll use `matplotlib` and `scipy` in the final section of the tutorial.
16+
17+
## Usage
18+
19+
After creating and activating your virtual environment, and installing the dependencies, you should be able to run each individual file normally:
20+
21+
```bash
22+
(venv) $ python filename.py
23+
```
24+
25+
You can find more information and context on the code blocks in [Using the NumPy Random Number Generator](https://realpython.com/numpy-random-number-generator/).

numpy-rng/array_constraints.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from numpy.random import Generator, PCG64DXSM
2+
3+
rng = Generator(PCG64DXSM())
4+
5+
print(rng.integers(size=(2, 2), low=1, high=5))
6+
print(rng.uniform(size=(2, 2), low=1, high=5))

numpy-rng/arrays.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import numpy as np
2+
3+
rng = np.random.default_rng()
4+
5+
print(rng.random(size=(5,)))
6+
print(rng.random(size=(5, 3)))
7+
print(rng.random(size=(3, 4, 2)))

numpy-rng/cars_per_minute.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import math
2+
3+
lam = 4
4+
cars_per_minute = [0, 4, 8]
5+
6+
for cars in cars_per_minute:
7+
probability = lam**cars * math.exp(-lam) / math.factorial(cars)
8+
print(f"P({cars}) = {probability:.1%}")

numpy-rng/choice.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import numpy as np
2+
3+
rng = np.random.default_rng()
4+
input_array_1d = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
5+
6+
print(rng.choice(input_array_1d, size=3, replace=False))
7+
print(rng.choice(input_array_1d, size=(2, 3), replace=False))

numpy-rng/choice_shuffle.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import numpy as np
2+
3+
input_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
4+
5+
rng = np.random.default_rng()
6+
print(rng.choice(input_array, size=2))
7+
print(rng.choice(input_array, size=2, axis=1))
8+
print(rng.choice(input_array, size=3, replace=False))
9+
10+
rng = np.random.default_rng(seed=100)
11+
print(rng.choice(input_array, size=3, replace=False, shuffle=False))
12+
13+
rng = np.random.default_rng(seed=100)
14+
print(rng.choice(input_array, size=3, replace=False, shuffle=True))
15+
16+
rng = np.random.default_rng(seed=100)
17+
print(rng.choice(input_array, size=3, replace=True, shuffle=False))
18+
19+
rng = np.random.default_rng(seed=100)
20+
print(rng.choice(input_array, size=3, replace=True, shuffle=True))

numpy-rng/constraints.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
contourpy==1.0.7
2+
cycler==0.11.0
3+
fonttools==4.39.4
4+
kiwisolver==1.4.4
5+
matplotlib==3.7.1
6+
numpy==1.24.3
7+
packaging==23.1
8+
Pillow==9.5.0
9+
pyparsing==3.0.9
10+
python-dateutil==2.8.2
11+
scipy==1.10.1
12+
six==1.16.0

numpy-rng/decks.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import numpy as np
2+
3+
4+
def create_deck():
5+
RANKS = "2 3 4 5 6 7 8 9 10 J Q K A".split()
6+
SUITS = "♣ ♢ ♡ ♠".split()
7+
return np.array([r + s for s in SUITS for r in RANKS])
8+
9+
10+
def create_high_cards():
11+
HIGH_CARDS = "10 J Q K A".split()
12+
SUITS = "♣ ♢ ♡ ♠".split()
13+
return np.array([r + s for s in SUITS for r in HIGH_CARDS])
14+
15+
16+
if __name__ == "__main__":
17+
print("Full deck:\n", create_deck(), end="\n\n")
18+
print("High cards:\n", create_high_cards(), end="\n\n")

numpy-rng/default_rng.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import numpy as np
2+
3+
default_rng = np.random.default_rng()
4+
print(default_rng)
5+
print(default_rng.random())
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from numpy.random import PCG64DXSM, Generator
2+
3+
pcg64dxsm_rng = Generator(PCG64DXSM())
4+
print(pcg64dxsm_rng.random())
5+
6+
rng1 = Generator(PCG64DXSM(seed=100))
7+
print(rng1.random())
8+
print(rng1.random())
9+
10+
rng2 = Generator(PCG64DXSM(seed=100))
11+
print(rng2.random())
12+
print(rng2.random())

0 commit comments

Comments
 (0)