smci
0
Q:

python random list

# To create a list of random integer values:
import random
randomlist = random.sample(range(10, 30), 5)
# Output:
# [16, 19, 13, 18, 15]

# To create a list of random float numbers:
import numpy
random_float_array = numpy.random.uniform(75.5, 125.5, 2)
# Output:
# [107.50697835, 123.84889979]
10
import random

list = ["Item 1", "Item 2", "Item 3"]			# List
item = random.choice(list)						# Chooses from list
print(item)					# Prints choice

# From stackoverflow
# Tried and tested method
5
import random
names=['Mark', 'Sam', 'Henry']

#Set any array
random_array_item=random.choice(names)

#Declare a variable as a random choice
print(random_array_item)

#Print the random choice
#Or, if you want to arrange them in any order:
for j in range(names):
  print(random.choice(names))
5
import random
n = random.randint(0,22)
print(n)
3
import random

random.choices(list, k = 4)
1
import random

# with replacement = same item CAN be chosen more than once.
# without replacement = same item CANNOT be chosen more then once.

# Randomly select 2 elements from list without replacement and return a list
random.sample(list_name, 2)

# Randomly select 3 elements from list with replacement and return a list
random.choices(set_name, k=3)

# Returns 1 random element from list
random.choice(list_name)
1
import random
n = random.random()
print(n)
1
import random
#Generate 5 random numbers between 10 and 30
# sample number needs to be smaller than population
randomlist = random.sample(range(10, 30), 5)
print(randomlist)
0
import random
randomlist = []
for i in range(0,5):
n = random.randint(1,30)
randomlist.append(n)
print(randomlist)
0

New to Communities?

Join the community