The following articles mainly introduce the Python Random Number Module modules through the code of the Python Random Number Module. If you understand these modules, it will be better used in the practical application of the Python random number module. The following is a detailed introduction of the article.
Random INTEGER:
- >>> import random
- >>> random.randint(0,99)
- 21
Randomly select an even number between 0 and 100:
- >>> import random
- >>> random.randrange(0, 101, 2)
- 42
Random Floating Point Number:
- >>> import random
- >>> random.random()
- 0.85415370477785668
- >>> random.uniform(1, 10)
- 5.4221167969800881
Random characters:
- >>> import random
- >>> random.choice('abcdefg&#%^*f')
- 'd'
Select a specific number of characters from multiple characters:
- >>> import random
- random.sample('abcdefghij',3)
- ['a', 'd', 'b']
In the Python Random Number Module, select a specific number of characters to form a new string:
- >>> import random
- >>> import string
- >>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
- eplace(" ","")
- 'fih'
Randomly selected string:
- >>> import random
- >>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
- 'lemon'
Shuffling:
- >>> import random
- >>> items = [1, 2, 3, 4, 5, 6]
- >>> random.shuffle(items)
- >>> items
- [3, 2, 5, 6, 4, 1]
The above article introduces the Python Random Number Module.