1# imports random
2import random
3# randint generates a random integar between the first parameter and the second
4print(random.randint(1, 100))
1#import random
2import random
3
4names = ['Harry', 'John', 'Smith', 'Larry']
5
6#print random name from names
7print(random.choice(names))
8
9#print random integer in a range of numbers
10print(random.randint(1, 100)
1# imports random
2import random
3# randint generates a random integar between the first parameter and the second
4print(random.randint(1, 100))
5# random generates a random real number in the interval [0, 1)
6print(random.random())
1#imports
2import random
3#randint generates a random number between the first set and the second set of parameters
4x = random.randint(1, 100)
5print(x)
1#Class Examples in random Module
2
3>>> random() # Random float: 0.0 <= x < 1.0
40.37444887175646646
5
6>>> uniform(2.5, 10.0) # Random float: 2.5 <= x < 10.0
73.1800146073117523
8
9>>> expovariate(1 / 5) # Interval between arrivals averaging 5 seconds
105.148957571865031
11
12>>> randrange(10) # Integer from 0 to 9 inclusive
137
14
15>>> randrange(0, 101, 2) # Even integer from 0 to 100 inclusive
1626
17
18>>> choice(['win', 'lose', 'draw']) # Single random element from a sequence
19'draw'
20
21>>> deck = 'ace two three four'.split()
22>>> shuffle(deck) # Shuffle a list
23>>> deck
24['four', 'two', 'ace', 'three']
25
26>>> sample([10, 20, 30, 40, 50], k=4) # Four samples without replacement
27[40, 10, 50, 30]
28