I want to create a list that is a random length of randomly chosen integers. What I have so far is:
a = [random.randint, random.randint, random.randint] and it goes on, but I want it so that it isn't however many times I typed "random.randint", but it uses the random function to generate a random number of random integers.
Add a comment
|
4 Answers
that's what I would do:
import random
a = random.randint(0,100)
b = []
for i in range(0,a):
b.append(random.randint(0,100))
1 Comment
Alan Aristizabal
This one makes a lot more sense, especially because I never learned random.sample and how it works.
Use the below code, bunch of random.randint and on single random.sample:
import random
l=[random.randint(0,100) for i in range(100)]
print(random.sample(l,random.randint(0,100)))
Output:
[27, 44, 80, 95, 54, 41, 52, 26, 21, 26, 91, 92, 10, 85, 13, 62, 30, 45, 0, 24, 58, 11, 95, 17, 0, 29, 37, 66]
Comments
Numpy if you're planning on a large scale (it will be faster than list comprehension)
import numpy as np
your_list = list(np.random.randint(0,100,n)) # n is the list size.
You can do away with list if you only need an iterable.
1 Comment
U13-Forward
I am not sure if he wants NumPy