Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

Make a 'list' called my_randoms of 10 irregular (random) numbers somewhere in the range of 0 and 100. 

This is the thing that I have up until now: 

import random

my_randoms=[]

for i in range (10):

    my_randoms.append(random.randrange(1, 101, 1))

    print (my_randoms)

But, I got output like this:

[34]

[34, 30]

[34, 30, 75]

[34, 30, 75, 27]

[34, 30, 75, 27, 8]

[34, 30, 75, 27, 8, 58]

[34, 30, 75, 27, 8, 58, 10]

[34, 30, 75, 27, 8, 58, 10, 1]

[34, 30, 75, 27, 8, 58, 10, 1, 59]

[34, 30, 75, 27, 8, 58, 10, 1, 59, 25]

It creates the 10 numbers as I ask it to, yet it produces it each in turn. What am I fouling up?

1 Answer

0 votes
by (26.4k points)

You can utilize random.sample to initialize the list within one call.

import random

my_randoms = random.sample(range(100), 10)

That produces numbers in the (comprehensive) range from 0 to 99. On the off chance that you need 1 to 100, you could utilize this

my_randoms = random.sample(range(1, 101), 10)

Join the python online course fast, to learn python concepts in detail and get certified.

Related questions

0 votes
1 answer
asked Jun 26, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 20, 2019 in Java by Anvi (10.2k points)
0 votes
4 answers
asked Mar 31, 2021 in Python by laddulakshana (16.4k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.5k answers

500 comments

108k users

Browse Categories

...