Generating Random Numbers According to Distributions

One more amazing feature of the random module is that it allows us to generate random numbers based on different probability distributions. There are various functions like gauss(), expovariate(), etc. which help us in doing this.

Generating Random Numbers According to Distributions

One more amazing feature of the random module is that it allows us to generate
random numbers based on different probability distributions. There are various
functions like gauss(), expovariate(), etc. which help us in
doing this. gauss() is a function of the random module used for generating random numbers according to a normal distribution.

It takes mean and standard deviation as an argument and returns a random
number:

for
_ in range(5):
print(random.gauss(0,1))

-1.3807849337411071
-0.7294464416591725
0.6896141700205516
0.20423084292338536
-0.09989911260411549
expovariate()

The exponential distribution is another very common probability distribution that is used. The expovariate() function is used for getting a random number according to the exponential
distribution. It takes the value of lambda as an argument and returns a value
from 0 to positive infinity if lambda is positive, and from negative infinity to 0 if lambda is negative:

print('Random
number from exponential distribution=>',random.expovariate(10))

Random
number from exponential distribution=> 0.026499587639200222

Solved Programs

1) Write a program to demonstrate selecting a subset of five items from a list of 20 integers.

#
select a random sample without replacement

from
random import seed

from
random import sample

#
seed random number generator
seed(1)

# prepare
a sequence
sequence
= [i for i in range(20)]
print(sequence)

#
select a subset without replacement
subset
= sample(sequence, 5)

print(subset)

Output
[0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[4, 18, 2, 8, 3]



2) Write a program to generate 10 random integer values between 50 and 1000.

from
random import seed

from
random import randint

#seed random number generator

seed(1)

#
generate some integers
for
_ in range(10):
value = randint(50, 1000)
print(value)

Output:

187
632
917
871
832
114
311
170
557
829


3) Write a program to generate a list of 20 integers and gives
five examples of choosing one random item from the list.

# choose a random element
from a list
from random import seed
from random import choice

# seed random number
generator
seed(1)

# prepare a sequence
sequence = [i for i in
range(20)]
print(sequence)

# make choices from the
sequence
for _ in range(5):
selection = choice(sequence)
print(selection)

Output
[0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 4 18 2 8 3



4) Write a program to generate 10 random floating point values.

#
generate random floating point values

from
random import seed

from
random import random

#
seed random number generator

seed(1)

#
generate random numbers between 0-1

for
_ in range(10):
value = random()
print(value)

0.13436424411240122
0.8474337369372327
0.763774618976614
0.2550690257394217
0.49543508709194095
0.4494910647887381
0.651592972722763
0.7887233511355132
0.0938595867742349
0.02834747652200631

5) Write a program to generate and print 10 Gaussian random
values.

#
generate random Gaussian values

from
random import seed

from
random import gauss

#
seed random number generator

seed(1)

#
generate some Gaussian values

for
_ in range(10):

value
= gauss(0, 1)

print(value)

Output

1.2881847531554629
1.449445608699771
0.06633580893826191
-0.7645436509716318
-1.0921732151041414
0.03133451683171687
-1.022103170010873
-1.4368294451025299
0.19931197648375384
0.13337460465860485

Previous PostHow to set up R Environment
Next PostElevate your online presence with our SEO, Optimize for Success