Posted By

denakitan on 10/06/11


Tagged

number python numbers random generate examples generation


Versions (?)

Python - Random Numbers - Examples


 / Published in: Python
 

Example of some random number generation mechanisms built-in Python. More details in the following link: http://docs.python.org/library/random.html

  1. import random
  2.  
  3. from decimal import Decimal
  4.  
  5. if __name__ == '__main__':
  6.  
  7. # the most basic random function. Generates a floating-point number between 0.0 and 1.0
  8. print random.random()
  9.  
  10.  
  11. # prints a random integer between the two numbers given
  12. # perfect to bet in the 'MEGA-SENA' lottery
  13. print random.randint(1, 60)
  14.  
  15. # random.uniform() returns a floating-points number between the two numbers given
  16. # prints this number converted to decimal after rounding it to four places
  17. FOURPLACES = Decimal(10) ** -4 # same as Decimal('0.0001')
  18. print Decimal(str(random.uniform(1.6, 2.0))).quantize(FOURPLACES)

Report this snippet  

You need to login to post a comment.