# Python Programming – Random module

In this Page, We are Providing Python Programming – Random module. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

## Python Programming – Random module

Random module

This module implements pseudo-random number generators for various distributions. Almost all module functions depend on the basic function random ( ), which generates a random float uniformly in the semi-open range [0 . 0 , 1 . 0 ). Python uses the “Mersenne Twister” as the core generator. However, Mersenne Twister being completely deterministic, it is not suitable for all purposes and is completely unsuitable for cryptographic purposes.

Functions for integers

random.randrange ( stop )

Return a randomly selected integer element from range ( 0 , stop ) .

>>> random . randrange ( 88 )
17
>>> random . randrange ( -88 )

Traceback ( most recent call last ) :
File "<pyshell#l>" , line 1 , in <module>
random . randrange ( -88 )
File " C : \ Python27 \ lib \ random . py " , line 191 , in randrange
raise ValueError , " empty range for randrange ( ) "
ValueError: empty range for randrange ( )

random . randrange ( start , stop [ , step ] )
Return a randomly selected integer element from range ( start , stop , step ) .

>>> random . randrange ( 3 , 100 , 5 )
83

random . randint ( a , b )
Return a random integer N such that a<=N<=b.

>>> random . randint ( 5 , 86 )
70

Functions for sequences

random . choice ( seq )
Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.

>>> random . choice ( ' abcdefghij ' )
' e '
>>> random . choice ( [ ' aa ' , ' bb ' , ' cc ' , 11 , 22 ] )
' cc '

random . shuffle ( x [ , random ] )
Shuffle the sequence x in place. The optional argument random is a O-argument function returning a random float in [ 0 . 0 , 1 . 0 ); by default, this is the function random ( ).

>>> items= [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
>>> random . shuffle ( items )
>>> items
[ 4 , 7 , 2 , 6 , 3 , 5 , 1 ]

random . sample ( population , k )
Return a k length list of unique elements chosen from the population sequence; used for random sampling without replacement. Return a new list containing elements from the population, while leaving the original population unchanged. If the population contain repeating elements, then each occurrence is a possible selection in the sample.

>>> random . sample ( [ 1 , 2 , 3 , 4 , 5 ] , 3 )
[ 4 , 5 , 1 ]

To choose a sample from a range of integers, use an xrange ( ) object as an argument. This is especially fast and space efficient for sampling from a large population.

>>> random . sample ( xrange ( 10000000 ) , 5 )
[ 2445367 , 2052603 , 975267 , 3021085 , 6098369 ]

Functions for floating point values

random . random ( )
Return the next random floating point number in the range [ 0 . 0 , 1 . 0 ).

>>> random . random ( )
0.6229016948897019

random . uniform ( a , b )
Return a random floating point number N, such that a<=N<=b for a<=b and b<=N<=a for b<a.

>>> random . uniform ( 0 . 5 , 0 . 6 )
0.5795193565565696

random . triangular ( low , high , mode )
Return a random floating point number N such that low<=N<=high and with the specified mode between those bounds. The low and high bounds default to 0 and 1, respectively. The mode argument defaults to the midpoint between the bounds, giving a symmetric distribution.

>>> random . triangular ( 2 . 8 , 10 . 9 , 7 . 5 )
6.676127015045406

random . betavariate ( alpha , beta )
Beta distribution; conditions of the parameters are alpha>0 and beta>0. Returned values range between 0 and 1.

>>> random . betavariate ( 2 . 5 , 1 . 0 )
0.543590525336106

random . expovariate ( lambd )
Exponential distribution; lambd is 1.0 divided by the desired mean. It should be nonzero. Returned values range from 0 to positive infinity; if lambd is positive, and from negative infinity to 0, if lambd is negative.

>>> random . expovariate ( 0 . 5 )
1.5287594548764503

random . gammavariate ( alpha , beta )
Gamma distribution (not the gamma function). Conditions of the parameters are alpha>0 and beta>0.

>>> random . gammavariate ( 1 . 3 , 0 . 5 )
0.5893587279305473

random . gauss ( mu , sigma )
Gaussian distribution; mu is the mean, and sigma is the standard deviation. This is slightly faster than the normalvariate () function defined below.

>>> random . gauss ( 0 . 5 , 1 . 9 )
-1.8886943114939512

random . lognormvariate ( mu , sigma )
Log normal distribution; if natural logarithm of this distribution is taken, a normal distribution with mean mu, and standard deviation sigma is received, mu can have any value, and sigma must be greater than zero.

>>> random . lognormvariate ( 0 . 5 , 1 . 9 )
4.621063728160664

random . normalvariate ( mu , sigma )
Normal distribution; mu is the mean, and sigma is the standard deviation.

>>> random . normalvariate ( 0 . 5 , 1 . 9 )
1.6246107732503214

random . vonmisesvariate ( mu , kappa )
mu is the mean angle, expressed in radians between 0 and 271, and kappa is the concentration parameter, which must be greater than or equal to zero. If kappa is equal to zero, this distribution reduces to a uniform random angle over the range 0 to 2K .

>>> random . vonmisesvariate ( 0 . 5 , 1 . 9 )
-0.4664831190641767

random . paretovariate ( alpha )
Pareto distribution; alpha is the shape parameter.

>>> random . paretovariate ( 0 . 5 )
60.471412103322585

random . weibullvariate ( alpha , beta )
Weibull distribution; alpha is the scale parameter and beta is the shape parameter.

>>> random . weibullvariate ( 0 . 5 , 1 . 9 )
0.9229896561284915

Alternative generators

Apart from Mersenne Twister, there are more core random number generator, such as generator based on “Wichmann-Hill” algorithm.

>>> rnd=random . WichmannHill ( )
>>> rnd . random ( )
0.4065226158909223
>>>
>>> rnd=random . SystemRandom ( )
>>> rnd . random ( )
0.46579102190832355