Monday, December 17, 2012

PYTHON 3 RANDOM MODULE

PYTHON 3 RANDOM GENERATORS



REVISED: Sunday, March 3, 2013




Python 3 Random Module.

I.  PYTHON 3 RANDOM MODULE INTRODUCTION

For non state sharing generators instantiate multiple random instances.

random.randint(a, b)

Returns a random integer N such that a <= N <= b.

random.choice(seq)

Returns a random element from the non-empty sequence seqIndexError is raised if seq is empty.

random.shuffle(x)


Shuffle the sequence x = [1, 2, 3...] in place.

II.  PYTHON 3 RANDOM MODULE EXAMPLE SOURCE CODE

from random import shuffle, choice, randint 

def main():

    # Example 1
    print('EXAMPLE 1:\n','The Python code randint(-10,10) provides a random integer between 10 and -10; e.g.,',randint(-10,10),'.\n')

    # Example 2
    pet = ('dog', 'cat', 'turtle')
    print('EXAMPLE 2:\n','Given the choice of pets is (\'dog\', \'cat\', \'turtle\') choice(s) randomly picks a pet; for example,',choice(pet),'.\n')

    # Example 3
    num = randint(1,10)
    print('EXAMPLE 3:\n','Given a random number between one and ten; e.g.,',num,'.')
    guess = int(input('Enter a number from one and ten and left mouse click OK:'))
    print('You try to determine the random number and pick',guess,'.')
    if num == guess:
        print('Your number was right!\n')
    else:
        print('Your number was wrong!\n')

    # Example 4
    print('EXAMPLE 4:\n','The total number of the two dice you roll is',randint(1,6) + randint(1,6),'.\n')

    # Example 5
    print('EXAMPLE 5:\n','You walk into a cave looking for gold.')
    if randint(1,2) == 1:
        print('You locate gold!','\n')
    else:
        print('You have fun but do not locate any gold.','\n')

     # Example 6
    cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ,13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52]
    print('EXAMPLE 6:\n','A deck of cards comes out of the box in the order:',cards,'\n')
    shuffle(cards)
    print('After shuffling the order of the deck of cards is:',cards,'\n')

if __name__ == '__main__':
    main()

III.  PYTHON 3 RANDOM MODULE EXAMPLE OUTPUT

>>> 
EXAMPLE 1:
 The Python code randint(-10,10) produces a random integer between -10 and 10; e.g., -7 .

EXAMPLE 2:
 Given the choice of pets is ('dog', 'cat', 'turtle') choice(pet) randomly selects one of the pets; e.g., cat .

EXAMPLE 3:
 Given a random number between one and ten; e.g., 8.
You try to determine the random number and pick 6.
Your guess was wrong!

EXAMPLE 4:
 The total number of the two dice you roll is 6 .

EXAMPLE 5:
 You walk into a cave looking for gold.
You find gold! 

EXAMPLE 6:
 A deck of cards comes out of the box in the order: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52]

After shuffling the deck of cards is in the following order: [29, 25, 42, 44, 50, 6, 11, 14, 12, 30, 2, 39, 15, 49, 20, 24, 51, 1, 28, 45, 52, 9, 38, 27, 8, 41, 33, 40, 16, 26, 10, 36, 23, 21, 7, 22, 37, 19, 3, 46, 13, 31, 32, 5, 34, 17, 43, 48, 4, 35, 18, 47]
>>> 

IV.  REFERENCES

The Python Tutorial by Guido van Rossum and Fred L. Drake, Jr., editor (2003)

Style Guide for Python Code by Guido van Rossum and Barry Warsaw (2001)

Enjoy the Python 3 Random Module!


Elcric Otto Circle






-->




-->




-->

















How to Link to My Home Page

It will appear on your website as:

"Link to: ELCRIC OTTO CIRCLE's Home Page"



No comments:

Post a Comment