Random Number Generation in Python Programming

Random number generation is the process of generating a number that cannot be predicted better than by a random chance.

pro_grammer
Nov 6 · 2 min read
Image credits: Shutterstock

Most computer generate pseudo random numbers which are not true random numbers. They use algorithms to generate random numbers.

Python uses Mersenne Twister algorithm for random number generation. In python pseudo random numbers can be generated by using random module.

Code for Random Numer Generation

from random import *print random()
from random import *print randint(10, 100)
from random import *print uniform(1, 10)
import randomlist = [‘1’,’4',’7',’Hi’]print random.choice(list)
import randomlist = [1,2,3,”4", “l90”, “hi”]random.shuffle(list)print (list)
import randomfor i in xrange(5):print random.randrange(0, 100, 2)
import randomstr = “helloworld”list = [‘a’,’b’,’c’,’d’,’e’,’f’,’o’,’l’]print random.sample(str, 4)print random.sample(list, 4)

Sample output:

['o', 'h', 'l', 'w']

['f', 'b', 'c', 'a']

Generate Random Number Using Seed

A seed is a number that is used to initialize a pseudo random number generator. Seed guarantees that if you start from same seed you will get the same sequence of random numbers.

import random

random.seed(5)

print(random.random())

print(random.random())

print(random.random())
0.62290169489

0.741786989261

0.795193565566

List of Functions in Python Random Module

Initialize the random number generator getstate() Returns an object capturing the current internal state of the generator.

Restores the internal state of the generator.

Returns a Python integer with k random bits.

Returns a random integer from the range.

Returns a random integer between a and b inclusive.

Return a random element from the non-empty sequence.

Shuffle the sequence.

Return a k length list of unique elements chosen from the population sequence.

By the following methodologies & code you will achieve a successful random number generation.

If you want to know more about random number generation then click here.


This Article is sourced from: mindmajix

MindMajix

We are here to transform the education system and to provide quality education to the students across the globe with our world-class IT online courses. Our team at Mindmajix are highly professional with real-time industry knowledge.

pro_grammer

Written by

MindMajix

MindMajix

We are here to transform the education system and to provide quality education to the students across the globe with our world-class IT online courses. Our team at Mindmajix are highly professional with real-time industry knowledge.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade