C1_W2_Lab01_Suggestion: use new random sample generator


In the Lab, the Python functions used to generate random samples are using functions from numpy.random that no longer have development support and have lower code performance compared to the newer implementation, as explained in the NumPy Documentation after version 1.17: [Random sampling (numpy.random) — NumPy v1.25.dev0 Manual] and [What’s New or Different — NumPy v1.25.dev0 Manual].

The new recommended code is as follows:

# Do this (new version)
from numpy.random import default_rng
rng = default_rng()
vals = rng.standard_normal(10)
more_vals = rng.standard_normal(10)

# instead of this (legacy version)
from numpy import random
vals = random.standard_normal(10)
more_vals = random.standard_normal(10)

In the future there will be also an update of the default random sample generator (default_rng()), which can lead to improvements on parallel calculations: [Upgrading PCG64 with PCG64DXSM — NumPy v1.23 Manual]

I’ve stumbled with this suggestion on StackOverflow trying to learn the differences between np.random.rand vs np.random.random_sample: [python - np.random.rand vs np.random.random - Stack Overflow]

Best regards,

Thanks for your suggestion.

Hello Iago!

Thank you for sharing this with us!