How To Generate Random Numbers Using NumPy ?


How To Generate Random Numbers Using NumPy ?

Table Of Contents:

  1. What Are Random Numbers?
  2. Examples Of Random Number Generators.

(1) What Are Random Numbers?

  • As the name suggests, a random number is a number chosen by chance — i.e., randomly, from a set of numbers.
  • All the numbers in a specified distribution have an equal probability of being chosen randomly.

(2) Random Numbers?

Syntax: Default Random Number Generators

np.random.default_rng()

Example: Initializing The Generator

rng = np.random.default_rng()

Using ‘random()’ Function To Generate Numbers

Syntax:

numpy.random.random(size=None)

Parameters:

  • size : int or tuple of ints, optional – Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.

Returns:

  • out : float or ndarray of floats – Array of random floats of shape size (unless size=None, in which case a single float is returned).

Example-1:Random Decimal Number Generator

rng.random()

Output:

0.03584145017167317

Example-2: Generating 10 Random Numbers

rng.random(10)

Output:

array([0.90486713, 0.40078114, 0.44952358, 0.18589626, 0.3175533 ,
       0.89347518, 0.03356966, 0.59438873, 0.33121976, 0.4530089 ])

Example-3: Generating multi-dimensional Random Numbers

rng.random((3, 2))

Output:

array([[0.77440885, 0.75916156],
       [0.42323151, 0.76035767],
       [0.42136301, 0.39422448]])

Example-4: Reproducing The Same Random Numbers

  • If you want to generate the same random numbers repeatedly, then you can use the seeding technique.
  • To seed the generator you need to pass an integer value to the Generator function.
inport numpy as np
rng_seed = np.random.default_rng(14245)
rng_seed.random()

Output:

0.944980436348342

Note:

  • When we run the code again, we will get the same random number.

Example-4: Generating Random Integers

  • We can use the integer() method to generate the random integers.

Syntax:

random.Generator.integers(low, high=None, size=None, dtype=np.int64, endpoint=False)

Parameters:

  • low: int or array-like of ints – Lowest (signed) integers to be drawn from the distribution (unless high=None, in which case this parameter is 0 and this value is used for high).
  • high: int or array-like of ints, optional – If provided, one above the largest (signed) integer to be drawn from the distribution (see above for behavior if high=None). If array-like, must contain integer values
  • size: int or tuple of ints, optional – Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.
  • dtype: dtype, optional – Desired dtype of the result. Byteorder must be native. The default value is np.int64.
  • endpoint: bool, optional – If true, sample from the interval [low, high] instead of the default [low, high) Defaults to False

Returns:

  • out: int or ndarray of ints – size-shaped array of random integers from the appropriate distribution, or a single such random int if size not provided.

Example-1:

rng.integers(low=0, high=10)

Output:

3

Example-2:

rng.integers(low=0, high=10, size=10)

Output:

array([1, 9, 2, 9, 5, 1, 4, 5, 3, 2], dtype=int64)

Example-5: Generating Statistical Distribution

  • We can use the standard_normal() method to generate the statistical distribution.

Syntax:

random.standard_normal(size=None)

Parameters:

  • size: int or tuple of ints, optional – Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.

Returns:

  • out: float or ndarray – 

    A floating-point array of shape size of drawn samples, or a single sample if size was not specified.

Example-1:

import numpy as np
rng = np.random.default_rng()
rng.standard_normal()

Output:

-0.8662744069697407

Example-2:

import numpy as np
rng = np.random.default_rng()
rng.standard_normal(20)

Output:

array([-1.065457  ,  1.35928533,  2.0422915 ,  0.21322883,  0.1687038 ,
       -0.50008546, -0.08658725, -0.50808513, -0.44901999,  0.55842963,
       -2.25624767,  1.08228704,  0.57186154,  0.24438807,  1.37391439,
       -1.2284484 ,  1.3928497 , -1.97238106,  0.11309411, -0.7474105 ])

Leave a Reply

Your email address will not be published. Required fields are marked *