Python

Python Random Number Generator

Published by
Mike Woodhill PhD
How to generate Random Numbers using Python’s Random Number Generator.
1) Let’s start with an easy example that generates a random number between 0 and 1 (copy and paste code.):

import random

print(random.random())

Note: you will always have to first import “random”, which is Python’s Random Number Library.
Then, random.random() generates a random number between 0 and 1 (Example: 0.6297148070155475)
The print command displays it to the user.
See belows program execution.

Python Random Number Generator


2) To generate a random integer between say 1 and 100 (including 1 and 100):

import random

print(random.randint(1, 100))

Here, random.randint() generates a random integer between 1 and 100 (Example: 83) and the print command displays again it to the user.


3) To generate a list of 5 random integers between 1 and 100 (copy and paste code again.):

import random

for _ in range(5):
 value = randint(0, 100)
 print(value)

Here, the for loop runs 5 times generates a random integer between 1 and 100 each time stored in variable called ‘value’.
Example: 73,12,94,44,63
The print command displays them again to the user.


4) To generate a list of 5 random items from a given list do this (copy and paste code again.):

import random

mylist = ["apple", "banana", "cherry"]

print(random.choices(mylist,  k = 5))

Here, random.choices() randomly selects a fruit 5 times.
Example: cherry, banana, banana, apple, banana
The print command displays them again to the user.


5) To shuffle a list of given items do this (copy and paste code again.):

import random

mylist = ["apple", "banana", "cherry"]
random.shuffle(mylist)

print(mylist)

Here, random.shuffle() shuffles the given fruits. (Example: {cherry, banana, apple} ) and the print command displays them again to the user.


6) To change the output of the random functions you would change the seed, for example use random.seed(10) or random.seed(82) before calling a random function.
Different seeds result in different random outputs for a truly random look;-)
Otherwise random numbers generated by computers are actually pseudo-random.

Try the above 6 examples for yourself using the Python Online Compiler below.



Or try the W3 School Python Compiler: Python Random Number Generator

Mike Woodhill PhD

As a long time instructor I would like you to benefit from my Step by Step Calculators. You may also schedule a live tutoring session to succeed in your class. 😉

Published by
Mike Woodhill PhD

Recent Posts

  • uncategorized

LaPlace Transform Calculator

Laplace Transform Calculator Enter your Function in terms t to find any Laplace Transform. How…

9 months ago
  • Parabola

Parabola Graphing Calculator

What is the purpose of the Parabola Graphing Calculator? It allows you to graph Parabolas.…

9 months ago
  • Mike

Contact Mike

For Questions, Bookings, etc : Chat with us. Email us : support@mikescalculator.com Or call us…

9 months ago
  • Tutoring

Schedule a Tutoring Session

For Questions, Bookings, etc : Chat with us. Email us : support@mikescalculator.com Or call us…

9 months ago
  • Quadratic Equations

Domain and Range of a Parabola

Table of Content (8 min. reading time) 1 - Step by Step Solver: Find the…

9 months ago
  • Algebra

Domain and Range Calculator

What is the Domain and Range? 1) The Domain is defined as the set of…

9 months ago
  • Statistics

What is the Mean in Math?

What does the Mean mean in Math? The Mean in Mathematics is just another word…

9 months ago
  • Statistics

Sample Standard Deviation

What is the Formula for Sample Standard Deviation ? The Formula for the Standard Deviation…

9 months ago
  • Statistics

Mean Median Mode Range Calculator

How do you find Mean, Median, Mode, Range, Standard Deviation and Variance? Mean = Average…

9 months ago
  • Algebra

Compute Discriminant – to discriminate between Real and Complex Solutions of a Quadratic Equation.

What you will learn in this mini lesson You will learn how to find the…

9 months ago
  • Algebra

How to Factor a Quadratic Equation and Solve It.

What you will learn in this mini lesson You will learn how to factor and…

9 months ago
  • Algebra

Standard Form of a Quadratic Function

What you need to know about Standard Form of a Quadratic Function The Quadratic Equation…

9 months ago
  • Vertex

Vertex Form in 5 Minutes (Formulas, Solvers, Examples)

What is Vertex Form of a Quadratic Equation? The Vertex Form is f(x) = a(x-h)²+k…

9 months ago
  • Algebra

Solve A Quadratic Equation by Completing the Square

What you will learn in this mini lesson You will learn how to solve Quadratic…

9 months ago
  • Algebra

STANDARD TO VERTEX FORM CALCULATOR

Table of Content (8 min. reading time) 1 - Step by Step Solver: Find the…

9 months ago
  • Vertex Form of a Parabola

VERTEX TO STANDARD FORM CALCULATOR

How to convert from Vertex to Standard Form? The Vertex Form of a Parabola is…

9 months ago
  • Quadratic Equations

The 3 Parabola Forms: Standard Form, Vertex Form and Factored Form.

What you will learn in this mini lesson You will learn how the 3 different…

1 year ago
  • Statistics

Standard Deviation to Variance

Standard Deviation to Variance Calculator What is the difference between Variance and Standard Deviation? Both…

1 year ago
  • Algebra

Quadratic Form Calculator

What you will learn in this mini lesson You will learn how to solve Quadratic…

2 years ago