2 views
in Python

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?

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.