/ 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
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import random from decimal import Decimal if __name__ == '__main__': # the most basic random function. Generates a floating-point number between 0.0 and 1.0 print random.random() # prints a random integer between the two numbers given # perfect to bet in the 'MEGA-SENA' lottery print random.randint(1, 60) # random.uniform() returns a floating-points number between the two numbers given # prints this number converted to decimal after rounding it to four places FOURPLACES = Decimal(10) ** -4 # same as Decimal('0.0001') print Decimal(str(random.uniform(1.6, 2.0))).quantize(FOURPLACES)
Comments
