Python Random Number – A Step by Step Tutorial for Beginners

[ad_1]

Here is a quick guide on Python’s random number. You can always refer to it whenever you need to generate a random number in your programs.

Python has a built-in random module for this purpose. It exposes several methods such as randrange(), randint(), random(), seed(), uniform(), etc. You can call any of these functions to generate a Python random number.

Usually, a random number is an integer, but you can generate float random also. However, you first need to understand the context as a programmer and then pick the right function to use.

A Python Random Number Tutorial for Beginners

Python random number tutorial

Python random number generator

Python random number generator is a deterministic system that produces pseudo-random numbers. It uses the Mersenne Twister algorithm that can generate a list of random numbers.

A deterministic algorithm always returns the same result for the same input.

It is by far the most successful PRNG techniques implemented in various programming languages.

A pseudo-random number is statistically random, but has a defined starting point and may repeat itself.

Let’s now check out some real examples to generate random integer and float numbers.

Randrange() to generate a random number

The randrange() function is available with the Python random module. It makes you produce a random number from a given range of values.

This function has the following three variations:

Syntax

# randrange() with all three arguments
random.randrange(begin, end, step counter)

While using this form of randrange(), you have to pass the start, stop, and step values.

# randrange() with first two arguments
random.randrange(begin, end)

With this form, you provide the start and stop, and the default (=1) is used as the step counter.

# randrange() with a single argument
random.randrange(end)

You only need to pass the endpoint of the range. The value (=0) is considered for the starting point and default (=1) for the step counter.

Also, please note the end/stop value is excluded while generating a random number. And this function produces the ValueError exception in the following cases:

  • If you have used a float range for the start/stop/step.
  • Or if the start value is more than stop value.

Let’s now check out a few examples.

Python random number between 0 and 100

import random as rand

# Generate random number between 0 to 100

# Pass all three arguments in randrange()
print("First Random Number: ", rand.randrange(0, 100, 1))

# Pass first two arguments in randrange()
print("Second Random Number: ", rand.randrange(0, 100)) # Default step = 1

# Or, you can only pass the start argument
print("Third Random Number: ", rand.randrange(100))     # Default start = 0, step = 1

The output is:

First Random Number:  3
Second Random Number:  36
Third Random Number:  60

Python random number between 1 and 99

import random as rand

# Generate random number between 1 to 99

# Pass all three arguments in randrange()
print("First Random Number: ", rand.randrange(1, 99, 1))

# Pass first two arguments in randrange()
print("Second Random Number: ", rand.randrange(1, 99)) # Default step = 1

The result is:

First Random Number:  18
Second Random Number:  77

Randint() for the inclusive range

The randint() function is somewhat similar to the randrange(). It, too, generates a random integer number from range. However, it differs a bit:

  • Randint() has two mandatory arguments: start and stop
  • It has an inclusive range, i.e., can return both endpoints as the random output.

Syntax

# random module's randint() function
random.randint(start, stop)

This function considers both the begin and end values while generating the random output.

Randint() random number generator example

import random as rand

# Generate random number using randint()

# Simple randint() example
print("Generate First Random Number: ", rand.randint(10, 100))

# randint() along with seed()
rand.seed(10)
print("Generate Second Random Number: ", rand.randint(10, 100))

rand.seed(10)
print("Repeat Second Random Number: ", rand.randint(10, 100))

The following is the result:

Generate First Random Number:  14
Generate Second Random Number:  83
Repeat Second Random Number:  83

Random() to generate a random float number

It is one of the most basic functions of the Python random module. It computes a random float number between 0 and 1.

This function has the following syntax:

Syntax

# random() function to generate a float number
random.random()

Generate random float number between 0 and 1

import random as rand

# Generate random number between 0 and 1

# Generate first random number
print("First Random Number: ", rand.random())

# Generate second random number
print("Second Random Number: ", rand.random())

The following is the output after execution:

First Random Number:  0.6448613829842063
Second Random Number:  0.9482605596764027

Seed() to repeat a random number

The seed() method allows preserving the state of random() function. It means that once you set a seed value and calls random(). Python then maps the given seed to the output of this operation.

So, whenever you call seed() with the same value, the subsequent random() returns the same random number.

Syntax

# random seed() function
random.seed([seed value])

The seed is an optional field. Its default value is None, which means to use the system time for random operation.

Seed() function with random() example

import random as rand

# Generate random number using seed()

# Using seed() w/o any argument
rand.seed()
print("Generate First Random Number: ", rand.random())
rand.seed()
print("Generate Second Random Number: ", rand.random())

# Using seed() with an argument
rand.seed(5)
print("Generate Third Random Number: ", rand.random())
rand.seed(5)
print("Repeat Third Random Number: ", rand.random())

Here is the execution result:

Generate First Random Number:  0.6534144429163206
Generate Second Random Number:  0.4590722400270483
Generate Third Random Number:  0.6229016948897019
Repeat Third Random Number:  0.6229016948897019

Please note that you can even pair up seed() with other Python random functions such as randint() or randrange().

Uniform() to generate random float numbers

The uniform() function computes a random float number from the given range. It takes two arguments to specify the range.

The first parameter is inclusive for random processing.

Syntax

# random uniform() function
random.uniform(start, stop)

This function provides a random result (r) satisfying the start <= r < stop. Also, please note that both the start and stop parameters are mandatory.

Uniform() function to generate a random number

import random as rand

# Generate random float number using uniform()

# Simple randint() example
print("Generate First Random Float Number: ", rand.uniform(10, 100))
print("Generate Second Random Float Number: ", rand.uniform(10, 100))

The result after execution is:

Generate First Random Float Number:  20.316562022174068
Generate Second Random Float Number:  27.244409254588806

Now, we have come to the end of this Python random number generator tutorial. And we hope that it would help you in leading in the right direction. You may also like to read the below post.

Generate random number list in python

[ad_2]

Leave a Comment