import random
print(random.randint(3, 7)) #Prints a random number between 3 and 7
array = [cars, bananas, jet]
print(random.choice(array)) #Prints one of the values in the array at random
# generate random integer values
from random import seed
from random import randint
# seed random number generator
seed(1)
# generate some integers
for _ in range(10):
value = randint(0, 10)
print(value)
### random number between 0 and 0.99999
print(random.random())
### random number between 5 and 8
print(random.uniform(5,8))
### random number with normal distribution (bell curve)
## random.normalvariate(standard deviation sigma)
print(random.normalvariate(5, 0.1))
### simulate dice roll
print(random.randint(1,6))
### random item from list
number = ['one', 'two', 'three']
print(random.choice(number))
import random #imports random module which is built-in
random_fruits = ['apple', 'mango', 'banana'] #make a list of fruits and store it in the variable
print(random.choice(random_fruits)) #print fruits randomly
import random
#random numbers from 1 to 10
print(random.randint(1,10)) #use of random.randint()
#random word from a list
print(random.choice(["a","b","c","d","e"])) #use of random.choice()
#random shuffle a list
random_list = ["a","b","c","d","e"]
random.shuffle(random_list) #use of random.shuffle()
print(random_list)
import random # Imports the random package so that the program has full access to random-based functions
start = 1 # Put your staring value here
end = 8 # Put your ending value here
number = random.randint(start, end) # Calls the randomint function of random to generate a random number
print(number) # Prints the number that was generated above
import random
# Random number, 0 through 10, INCLUDING 10:
print(random.randint(0, 10))
#Random number, 0 through 10, NOT INCLUDING 10:
print(random.randrange(0, 10))
#Random choice from a list:
my_list = ["Apples", "Oranges", "Watermelon", "Pineapple", "Grapes"]
print(random.choice(my_list))
# Set a custom seed of 10 for random to work with:
random.seed(10)
# We'll be using the random module for this code
import random
# Let's first get the range of numbers from which they want a random number
start = int(input('What is the lowest number you would expect'))
end = int(input('What is the largest number you would expect'))
print('This is the number',random.randint(start,end))
"""Generation of non-unique 1d_lst and 2d_lst
- Function takes in a list of info about the non-unique list
- You can also import randint from random.
- Note that randint is inclusive and randrange is exclusive.
- If you are looking to generate unique lsts...
...you might want to use random.sample() on a lst of numbers.
"""
from random import randrange
dft = [10, 10, 100, 10] # dft = default (range, low, high, row)
def rand_1d(info = dft[:-1]):
r, low, high = info # r: range
return [randrange(low, high) for num in range(r)]
def rand_2d(info = dft):
*info_1d, rows = info
return [rand_1d(info_1d) for i in range(rows)]
## Test
randlst_gen = {
"rand_1d": rand_1d,
"rand_2d": rand_2d
}
r1, r2 = randlst_gen.values() # get the functions
# #function 1
# print("rand_1d --------------------------------------------------")
# print(f"default: {r1()}")
# print(f"rand: {r1([5, 3, 234])}")
# print()
# # function 2
# print("rand_2d --------------------------------------------------")
# print("default:")
# for row in r2():
# print(row)
# print()
# print("rand:")
# for row in r2([8, 11, 30, 5]):
# print(row)