Skip to content

Commit 014b568

Browse files
authored
Merge pull request #69 from UoMResearchIT/15-new-method-of-numpy-rng
Specify and use a numpy RNG
2 parents 1fed748 + bb98bf2 commit 014b568

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

episodes/03-numpy_essential.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,13 @@ plt.show()
241241

242242
## Noisy Signal
243243

244-
Now we are going to add some random noise to that curve. To do it we can use the NumPy function `normal` from the module `random` provided by NumPy library. We will scale the magnitude of the noise so it is (roughly) a 10th of the magnitude of the Gaussian maximum:
244+
Now we are going to add some random noise to that curve. To do this we need to create a Random Number Generator (RNG), which we can then use with the function `normal` from the module `random` provided by the NumPy library. We create an array of zeros of the same length as the array for our curve `g` as the mean values for the random noise, and then scale the magnitude of the noise so it is (roughly) a 10th of the magnitude of the Gaussian maximum. Finally, we then plot the result of adding the noise to the original curve:
245245

246246
```python
247-
noisy = np.random.normal(g, scale=g.max() / 10)
247+
rng = np.random.default_rng()
248+
zeros = np.zeros(len(g))
249+
noise = rng.normal(zeros, g.max() / 10)
250+
noisy = g + noise
248251
plt.plot(x, noisy)
249252
plt.show()
250253
```

0 commit comments

Comments
 (0)