2

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.

4 Answers 4

2

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))
Sign up to request clarification or add additional context in comments.

1 Comment

This one makes a lot more sense, especially because I never learned random.sample and how it works.
1

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

1

i think that will solve your problem choice

s = [random.randint (0, 100) for i in range(5)]
print (s)

Comments

0

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

I am not sure if he wants NumPy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.