Nick
0
Q:

random module in python

# imports random
import random
# randint generates a random integar between the first parameter and the second
print(random.randint(1, 100))
34
#Class Examples in random Module

>>> random()                             # Random float:  0.0 <= x < 1.0
0.37444887175646646

>>> uniform(2.5, 10.0)                   # Random float:  2.5 <= x < 10.0
3.1800146073117523

>>> expovariate(1 / 5)                   # Interval between arrivals averaging 5 seconds
5.148957571865031

>>> randrange(10)                        # Integer from 0 to 9 inclusive
7

>>> randrange(0, 101, 2)                 # Even integer from 0 to 100 inclusive
26

>>> choice(['win', 'lose', 'draw'])      # Single random element from a sequence
'draw'

>>> deck = 'ace two three four'.split()
>>> shuffle(deck)                        # Shuffle a list
>>> deck
['four', 'two', 'ace', 'three']

>>> sample([10, 20, 30, 40, 50], k=4)    # Four samples without replacement
[40, 10, 50, 30]
2
# imports random
import random
# randint generates a random integar between the first parameter and the second
print(random.randint(1, 100))
# random generates a random real number in the interval [0, 1)
print(random.random())
19
import random
5
#import random 
import random

names = ['Harry', 'John', 'Smith', 'Larry']

#print random name from names
print(random.choice(names))

#print random integer in a range of numbers
print(random.randint(1, 100)
6
import random
a = (random.randint(bottom value, top value)
3
print(random.randint(1, 100))
print(random.random())
3
from random import randint # Import randint from random
print(randint(1,20)) # Gets random number from first parameter to the second
0
import random
lower_limit = 0 # Can be anything
upper_limit = 100 # Again, can be anything
# Print it out
print(f"Here is a random number: {random.randint(lower_limit, upper_limit)}
1

New to Communities?

Join the community