Using the NumPy Random Number Generator :
by:
blow post content copied from Real Python
click here to view original post
Random numbers are a very useful feature in many different types of programs, from mathematics and data analysis through to computer games and encryption applications. You may be surprised to learn that it’s actually quite difficult to get a computer to generate true randomness. However, if you’re careful, the NumPy random number generator can generate random enough numbers for everyday purposes.
Maybe you’ve already worked with randomly generated data in Python. While modules like random
are great options for producing random scalars, using the numpy.random
module will unlock even more possibilities for you.
In this tutorial, you’ll learn how to:
- Generate NumPy arrays of random numbers
- Randomize NumPy arrays
- Randomly select parts of NumPy arrays
- Take random samples from statistical distributions
Before starting this tutorial, you should understand the basics of NumPy arrays. With that knowledge, you’re ready to dive in.
Free Bonus: Click here to download the sample code that shows you how to get random numbers with NumPy.
Understanding the NumPy Pseudo-Random Number Generator
When you ask a computer to perform any task for you, it does so by following a set of instructions defined by an algorithm. When you need it to generate random numbers, the computer uses a pseudo-random number generator (PRNG) algorithm. There are several of these available, some of which are better than others.
To generate random numbers, Python uses the random
module, which generates numbers using the Mersenne twister algorithm. While this is still widely used in Python code, it’s possible to predict the numbers that it generates, and it requires significant computing power.
Since version 1.17, NumPy uses the more efficient permuted congruential generator-64 (PCG64) algorithm. This produces less-predictable numbers, as shown by its performance in the industry-standard TestU01 statistical test. PCG64 is also faster and requires fewer resources to work.
Note: While the PCG64 algorithm is certainly an improvement on the Mersenne twister algorithm, it still has some statistical weaknesses. An updated version, PCG64DXSM, addresses these issues. This will become the default in future NumPy releases. On a practical level, you won’t notice any difference, but if you want to know more, see Upgrading PCG64
with PCG64DXSM
in the NumPy documentation.
In most of the examples throughout this tutorial, you’ll use the default PCG64 algorithm, although you’ll also try your hand at using the updated PCG64DXSM algorithm.
If you’re interested in learning more about the different types of PRNG algorithms and how the PCG algorithms compare to others, then you should read PCG, A Family of Better Random Number Generators from the developer of PCG.
PRNGs are called pseudo-random because they’re not random! PRNGs are deterministic, which means they generate sequences of numbers that are reproducible. PRNGs require a seed number to initialize their number generation. PRNGs that use the same seed will generate the same numbers.
PRNGs also have a period property, which is the number of iterations they go through before they start repeating. Because the generated numbers depend on the seed, they’re not truly random but are instead pseudo-random.
Because seeds should be random, you need one random number to generate another. For this purpose, PRNGs use the computer hardware clock’s time as their default seed. This is measured to the nanosecond, so running number generators consecutively results in different seed values and therefore different sequences of random numbers. NumPy uses a hashing technique to ensure that the seed is 128 bits long, even if you only supply a 64-bit integer.
The period does mean that the same numbers could reappear. In practice, this isn’t a concern because the period lengths are huge. The period of PCG64, for example, is about 50 billion times the number of atoms that exist inside of you!
Note: If you want to learn more about how random randomly generated numbers actually are, take a look at the tutorial How Random is Random?
The core of NumPy’s number generation is the BitGenerator
class. This class allows you to specify an algorithm and seed. To access the random numbers, the BitGenerator
is passed into a separate Generator
object. Generators have methods that allow you to access a range of random numbers and perform several randomizing operations. The numpy.random
module provides this capability.
You may have noticed that the NumPy.random
documentation also contains information about the RandomState
class. This is a container class for the slower Mersenne twister PRNG. The more modern Generator
class has now superseded RandomState
, which you should no longer use in new code. However, RandomState
is still around for existing legacy applications.
Before you go any further, be aware that the NumPy PRNGs are not suitable for cryptographic purposes. They’re only suitable for data analysis tasks. If you need random numbers for cryptographic purposes, then you need a cryptographically secure pseudo-random number generator (CSPRNG).
Generating Random Data With the NumPy Random Number Generator
Now that you understand a computer’s capabilities for generating random numbers, in this section, you’ll learn how to generate both floating-point numbers and integers randomly using NumPy. After generating individual numbers, you’ll learn how to generate NumPy arrays of random numbers.
Random Numbers
If you’re happy to let NumPy perform all of your random number generation work for you, you can use its default values. In other words, your BitGenerator
will use PCG64 with a seed from the computer’s clock. To facilitate the defaults, NumPy provides a very handy default_rng()
function. This sets everything up for you and returns a reference to a Generator
object for you to use to produce random numbers using its range of powerful methods.
To begin with, this code generates a floating-point number using NumPy’s defaults:
>>> import numpy as np
>>> default_rng = np.random.default_rng()
>>> default_rng
'Generator(PCG64) at 0x1E9F2ABBF20'
>>> default_rng.random()
0.47418635476614734
Read the full article at https://realpython.com/numpy-random-number-generator/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
June 05, 2023 at 07:30PM
Click here for more details...
=============================
The original post is available in Real Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================
Post a Comment