USING RANDOM MODULE
To use Random number generators we have to import
random module first. There are basically four set of random
generator function in this module, they are-
1. random ( )- it generates floating point numbers between
0.0 to less than 1.0 . Only 0.0 is included in the output
but 1.0 is excluded. It is a function without any
parameter.
Example-
import random
print([Link]( )*5) # 0.0 to <5
output- 0.0 to 4.999999
2. randint(a,b)- it generates a random integer value where
the number is between a to b. Here both the values i.e.
a & b will also be included in the output, but in a
random manner.
Example-
Import random
print([Link](3,10))
Output- 5,3,8,6,9,7,4,10
print([Link](3,10) -2)
Output- Minimum Value- 1 and Maximum Value-8
Overall output- 7,6,1,3,5,2,8,4
3. [Link](a,b)- it generates a random floating
point value where the number is between a to b or b to
a . Here both the values i.e. a & b will also be included in
the output, but in a random manner.
Import random
print([Link](3.0,10.0)) #a<b
Output- 4.48765
print([Link](6.0,2.0)) #a>b
Output- 5.714678
4. [Link](stop) &
[Link](start,stop[,step])- it generates a
random integer value from start to stop-1 and step is
used for forward jump(+ve) and backward jump(-ve).if
only stop parameter is taken, then the output will be
from 0 to stop-1. In all the cases only start value will be
included in the output, but stop value will not be
included and the output will be in a random manner.
Import random
print([Link](9))
#Output- a number between 0 to 8 will be displayed in
#random manner
print([Link](2,9))
#Output- a number between 2 to 8 will be displayed in
#random manner
print([Link](2,9,2))
#Output- a number between 2 to 8 will be displayed in
#random manner with jump of 2 (2, 4, 6, 8)
print([Link](9,2-2))
#Output- a number between 9 to 3 will be displayed in
#random manner with jump of -2 (9, 7, 5, 3)