Return to Snippet

Revision: 51856
at October 6, 2011 06:42 by denakitan


Initial Code
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)

Initial URL

                                

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

Initial Title
Python - Random Numbers - Examples

Initial Tags
number, python

Initial Language
Python