Tuesday, May 20, 2014

Generate random numbers in Python

Python random module helps to generate random numbers.
We can generate this in three ways
  • randint(a, b)
  • uniform(a, b)
  • random()
randint(a, b) : returns a random integer number in the range [a, b]
uniform(a, b) : returns a floating point number in the range [a, b]
random() : returns a floating point number in the range [0, 1]

>>> import random
>>> a = random.randint(1,99)
>>> a
77
>>> b = random.uniform(1,999)
>>> b
766.6455229055509
>>> c = random.random()
>>> c
0.6372421095384033
>>>

Labels: ,


0 comments: Post Yours! Read Comment Policy ▼
PLEASE NOTE:
We have Zero Tolerance to Spam. Chessy Comments and Comments with Links will be deleted immediately upon our review.

 
Twitter Facebook RSS YouTube Google
© 2014 | Distributed and Designed By Jasad Moozhiyan