random number generator in python

Best Random Number Generator in Python along with source along with 3 amazing project

Spread this useful information with your friends if you liked.

Best Random Number Generator in Python along with source along with 3 amazing project

Do you have doubts regarding how to create a Random Number Generator in Python?

Don’t worry, in this article, we will provide you with a detailed explanation of how to use the Random Number Generator in Python along with various important concepts in it.

Here are some of our recommended articles that you might find interesting:

Top 10+ Best Book for Python Programming Language in 2021

Best 15 Python Project for Beginners and Intermediate Level Programmers in 2021

So let’s start this post with the table of contents.

What is Pseudo Random Number Generator (PRNG)?

A Pseudo Random Number Generator (PRNG) also known as a Deterministic Random bit generator is an algorithm used for generating a variable from a sequence of numbers with similar properties as a random number.

The PRNG generates a huge sequence of numbers from an initial number known as Seed. The sequence generated from PRNG is repeated after a long period.

Pseudo Random Number Generator in Python is mainly used for applications like simulations and Gaming.

If you are willing to learn more about pseudo-random number generators watch this amazing video.

How to create a Random Number Generator in Python?

Python has a variety of built-in modules with various functionalities; Random is also a popular built-in module in Python that we will use for generating a random number in Python.

Random module comprises many functions that are used for performing operations like generating a random number, selecting a random element from a list, shuffle elements randomly from a list and many more.

 
Generating a random floating-point number:

There are two methods for generating a random floating-point number using random module

  1. Using random() function [0-0.9…]
  2. Using uniform() function [within a range]

Using random() function:

The function random() is used to generate a random number that ranges between 0 and 0.9999999999999999 but never reaches 1.0.

Syntax:

random.random()

The function random() generates a float by using Mersenne Twister (MT) as the core generator. The number generated using this function ranges between [0, 0.9999999999999999] with 16 decimal place numbers.

Code:

import random
print(random.random())

Output:

0.8063942182254681

Using uniform() function

For generating a random float number we will use uniform() function from the random module.

Syntax:

random.uniform(a, b)
//a and b are numeric variables.

The function uniform() needs to be provided with two arguments that work as a range for the random float number that is to be generated.

Code:

import random
print(random.uniform(1, 10))

Output:

6.797725973630237

Generating a random integer within a given range

For generating a random integer we will use randint() function from the random module.

Syntax:

random.randint(a, b)
//a and b are numeric variables.

The function randint() needs to be provided with two arguments that work as a range for the random integer number that is to be generated.

Consider n as the variable that is generated, n can be represented as

a <= n <= b or n Є [a, b]

Code:

import random
print(random.randint(5, 100))

Output:

71

Practice project involving Random number generation.

 
Roll the Dice

Create a python program that will represent a dice by generating random integer numbers from 1 to 6.

Source code:

import random 

while True: 
    print("Rolling dice.....") 
    number = random.randint(1,6) 
    print(f"The dice number is : {number} ") 
    choice = input("Do you want to continue rolling dice? (y/n) : ") 
    if choice.lower() == 'n': 
        break rolldice()

Solution video:

Love Calculator

This is a fun project and a great way to practice your Python programming skills.

Generate a Love calculator by using a random module in Python. Input names of the partners  and also add a suitable statement using conditional statements based on generated output.

Source code:

import random

def lovecalculator(partner_a, partner_b):
    number = random.randint(0, 100) 
    if number <= 40: 
        print(f"The score is : {number} \n {partner_a} and {partner_b} are not suitable for each other")
    elif number <= 80: 
        print(f"The score is : {number} \n {partner_a} and {partner_b} might be a good choice for each other") 
    else:
        print(f"The score is {number} \n {partner_a} and {partner_b} are made for each other")


a = input("Enter Your Name : ") 
b = input("Enter The Name Of Your Partner : ") 
lovecalculator(a, b)
Potential number of friends

Generate a calculator that  will randomly generate a number from 1 to 200 using a random module. Also take the name of the user as input and add a suitable print statement indicating the user about potential friends this year.

import random 

def friend(your_name): 
    number = random.randint(1, 200) 
    print(f"You will make {number} in this year!!!")
    
    
a = input("Enter Your Name : ") 
friend(a)

Applications of Random Number Generators

Random Number Generators have a variety of applications in many different fields including science, art, gaming, cryptography, gambling, statistics, etc.

But, As PRNG doesn’t generate complete random numbers and numbers are repeated after a period it cannot be used for some applications. PRNG are used mainly in gaming for increasing randomness and statistics.


Spread this useful information with your friends if you liked.

Leave a Comment

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