Python # Python Random Number Generator

Published by

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.

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

Published by

2 years ago

- uncategorized

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

9 months ago

- Parabola

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

9 months ago

- Mike

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

9 months ago

- Tutoring

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

9 months ago

- Quadratic Equations

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

9 months ago

- Algebra

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

9 months ago

- Statistics

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

9 months ago

- Statistics

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

9 months ago

- Statistics

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

9 months ago

- Algebra

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

9 months ago

- Algebra

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

9 months ago

- Algebra

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

9 months ago

- Vertex

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

9 months ago

- Algebra

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

9 months ago

- Algebra

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

9 months ago

- Vertex Form of a Parabola

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

9 months ago

- Quadratic Equations

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

1 year ago

- Statistics

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

1 year ago

- Algebra

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

2 years ago