Site icon DataFlair

Python Random Number – Generate Random Numbers With NumPy

Generating Python Random Number

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Today, in this Python tutorial, we will talk about Python random numbers.

We will see ways to generate and import random numbers in Python. Also, we will discuss generating a Python Random Number with NumPy. 

So, let’s begin.

Need of Python Random Number

A Random Number in Python is any number in a range we decide.

From initializing weights in an ANN to splitting data into random train and test sets, the need for generating random numbers is apparent.

Use cases of Random Number:

So today, we will discuss pseudorandom generators in Python. We will also try doing that with the standard Python library and with NumPy.

How to Generate a Python Random Number?

What we really generate are pseudorandom numbers. These are numbers that appear nearly random but are actually something we generate with a deterministic process.

Python uses the Mersenne Twister pseudorandom number generator.

The process of generating random numbers involves deterministically generating sequences and seeding with an initial number.

The default for the seed is the current system time in seconds/ milliseconds. A different seed will produce a different sequence of random numbers.

1. Import the Random module

Before we can begin, let’s first import the module random from the Python Standard Library. You can directly import it-

import random

To import a piece of functionality from it- say, random, you can:

from random import random

Or for seed, you can:

from random import seed

2. Random Floating Point Numbers

Let’s take an example of generating a Python Random Number.

>>> from random import seed
>>> from random import random
>>> seed(7)
>>> random(),random(),random(),random()

Output

(0.32383276483316237, 0.15084917392450192, 0.6509344730398537, 0.07243628666754276)

Works for us. Now, what if we reseed to the same value and call the random() functions/methods again?

>>> seed(7)
>>> random(),random(),random(),random()

Output

(0.32383276483316237, 0.15084917392450192, 0.6509344730398537, 0.07243628666754276)

You’ll find it gives us the same thing as it did earlier.

Some days, you may not want to generate a random number in Python values between 0 and 1.

In the following piece of code, 2 is the minimum value, and we multiply the random number generated by 10.

>>> seed(7)
>>> 2+10*random()

Output

5.238327648331624

3. Python Random Integers

We use the randint() function to get integers randomly.

It takes two arguments- the start and the top, and then draws a random value from a uniform distribution. Each value has an equal chance of being picked.

>>> from random import randint
>>> seed(7)
>>> randint(0,9),randint(0,9),randint(0,9)

Output

(5, 2, 6)

We asked for three random values; this gave us 5, 2, and 6.

4. Getting Integers Randomly from a Range

randrange() randomly selects an element from range(start,stop,step).

>>> from random import randrange
>>> randrange(-2,4)

Output

-1
>>> randrange(-2,4)

Output

0
>>> randrange(-2,4)

Output

1
>>> randrange(-2,4)

Output

-1
>>> randrange(-2,4)

Output

2
>>> randrange(-2,4)

Output

-2

5. Random Gaussian Values in Python

The gauss() function takes in two arguments- the mean and the standard deviation. This gives us a real-valued distribution.

>>> from random import gauss
>>> seed(7)
>>> gauss(0,1),gauss(0,1),gauss(0,1)

Output

(-0.2558802884476004, 0.511431512516514, -0.2260961647831047)

6. Choosing Randomly From Lists

It is possible to randomly pick values from our own custom lists. We have the choice() function/method for this.

>>> list=[2,4,3,9,6,2,1,0,7,4,3,5,3,6,8]
>>> from random import choice
>>> seed(7)
>>> choice(list),choice(list),choice(list),choice(list),choice(list),choice(list)

Output

(2, 3, 1, 3, 2, 4)

For a choice of multiple values, you can use choices() instead.

>>> from random import choices
>>> choices(list,k=4)

Output

[7, 2, 9, 7]

7. Randomly Choosing a Subset from a List

Once sample() puts an item from a list into the sublist, it does not add it back to the original list for picking from.

This is a selection without replacement. Note that this does not modify the original list. This function/ method also takes the size of the subset to create.

>>> list

Output

[2, 4, 3, 9, 6, 2, 1, 0, 7, 4, 3, 5, 3, 6, 8]
>>> from random import sample
>>> sample(list,6)

Output

[6, 7, 4, 2, 4, 2]

8. Shuffling a List Randomly

We can shuffle a list like a deck of cards with the shuffle() function/ method.

This shuffles the list in-place. In other words, it does not need to create a new list to put shuffled items into one by one.

>>> list

Output

[2, 4, 3, 9, 6, 2, 1, 0, 7, 4, 3, 5, 3, 6, 8]
>>> from random import shuffle
>>> shuffle(list)
>>> list

Output

[6, 3, 2, 3, 6, 5, 0, 3, 4, 1, 4, 2, 9, 7, 8]

Note that this modifies the list.

How to Generate a Python Random Number with NumPy?

With the seed() and rand() functions/ methods from NumPy, we can generate random numbers. The functionality is the same as above.

>>> from numpy.random import seed
>>> from numpy.random import rand
>>> seed(7)
>>> rand(3)

Output

array([0.07630829, 0.77991879, 0.43840923])
>>> seed(7)
>>> rand(3)

Output

array([0.07630829, 0.77991879, 0.43840923])

Python Interview Questions on Random Numbers

1. How to choose a random number from a list in Python?

2. How to generate a random number using NumPy?

3. Explain the way to get Random Gaussian Numbers.

4. How does Python generate Random Numbers?

5. What is the need to generate a random number in Python?

Conclusion

Python’s random module supplies pseudo-random numbers built on the Mersenne Twister engine, giving 53-bit precision and a long period. Functions like random(), randint(a, b), and choice(seq) let you pick floats, ints, or items with ease.

Exit mobile version