0

I want to create 10,000 random numbers withing the range of 1 to 100.

How can I do with python?

I can use for loop and with each for loop I can use random.sample() or random.random() function.

Is there any other way, without using any for loop I can generate it with built-in function ?

7
  • [rand.randrange(1,100) for _ in range(10000)] should do. But maybe you've got demands for no-repeats, etc... we don't know Commented Aug 6, 2018 at 12:39
  • What is wrong with using random.sample(range(1,100), 10000) exactly? (for sampling without replacement or random.choices() with replacement) Commented Aug 6, 2018 at 12:39
  • Now, when you say "I can use random.sample or random.random() function.", do you mean "I know I can use sample() or random(), but I am receptive to using other functions from the random module", or do you mean "for this assignment, I am allowed to use only sample() and random(), and all other functions are off limits, which is why I'm not using randint()"? Commented Aug 6, 2018 at 12:40
  • @Chris_Rands with random.sample(range(1,100), 10000) you will get error ValueError: Sample larger than population or is negative Commented Aug 6, 2018 at 12:42
  • @Jean-FrançoisFabre I can use for loop but i want to avoid it Commented Aug 6, 2018 at 12:45

1 Answer 1

4

For python 3.6:

import random

my_randoms = random.choices(range(1,100), k=10000)
print(my_randoms)

For older would still need to use loops:

my_randoms = [random.randrange(100) for x in range(10000)]
Sign up to request clarification or add additional context in comments.

2 Comments

thanks. Its working perfectly in python 3. I can use it. Do you know anything similar in python 2. As I go through python 2 documentation. I didn't found any.
can you specify the min version of python to use this? it is at least 3.5 or 3.6

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.