1# imports random
2import random
3# randint generates a random integar between the first parameter and the second
4print(random.randint(1, 100))
1import random
2print(random.randint(3, 7)) #Prints a random number between 3 and 7
3array = [cars, bananas, jet]
4print(random.choice(array)) #Prints one of the values in the array at random
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#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