Laire
Laire

Reputation: 35

Generate 100 numbers at once and make it a list and add it to an empty list

I need to generate 100 numbers from 1-500 and append it to an empty list, but I don't know how to fix my code. There is a <'none'> at the end when I print it. And when I used a len function to count a, it only says 1, so it didn't really added the 100 numbers.

a = []
import random
print("original: \n")
for x in range(100):
    nums = random.randint(1,501)
    b = print(nums, end=" ")

a.append(b)
print(a)
print(len(a))

Upvotes: 1

Views: 1782

Answers (5)

realmanusharma
realmanusharma

Reputation: 340

Your code was correct just a little mistake that is you don't have indented the a.append(nums), So that become out side the scope of for loop. Try this code :) .

import random

a = []
print("original: \n")

for x in range(100):
    nums = random.randint(1,501) 
    print(nums, end=" ") 
    a.append(nums) 

print(a) 
print(len(a))

Upvotes: 0

Usman
Usman

Reputation: 2029

randint() function generate only one random number. You can generate the random in a for loop 100 times & simulatenosuly append in the list so when outside the for loop ,the list 'a' have the 100 random numbers & this can be verified through length len() function .

Also I am attach the screenshot of the output.

a = []
import random
print("original: \n")
for x in range(100):
  nums = random.randint(1,501)
  a.append(nums)

print("Numbers : " , a)
print("Length : " , len(a))

enter image description here

Upvotes: 0

jpp
jpp

Reputation: 164773

I believe this is what you are looking for. print does not return anything, you cannot add print onto a list. In fact, it's not clear why you need 2 variables for this.

import random

a = []

for x in range(100):
    a.append(random.randint(1,501))

print(' '.join(map(str, a)))

print(len(a))

An alternative solution which avoids loops uses the numpy library:

import numpy as np

a = np.random.randint(1, 501, 100)

If performance is an issue; e.g. for many, large random lists; here is benchmarking versus a valid list-based solution using random:

import numpy as np
import random

%timeit np.random.randint(1, 501, 100)        # 5.97 µs
%timeit random.choices(range(1, 501), k=100)  # 66.3 µs

Upvotes: 3

cs95
cs95

Reputation: 402844

On python-3.6, you can use random.choices, and extend a by this function's return value:

a.extend(random.choices(range(1, 501), k=100))

Although, if you're starting off with an empty list and adding nothing before this step, it would make more sense to just initialise a as

a = random.choices(range(1, 501), k=100)

Without doing any empty-list initialisation before.

If you're on older versions of python, use random.randint in a loop.

a.extend(random.randint(1, 500) for _ in range(100))  

Or,

a = [random.randint(1, 500) for _ in range(100)]

Alternatively,

a = []
for _ in range(100):
    a.append(random.randint(1, 500))

From the docs, I believe the upper bound is inclusive (hence, 500).

Upvotes: 3

poke
poke

Reputation: 388063

nums = random.randint(1,501)

This returns a single random number, so the plural name “nums” is misleading here.

b = print(nums, end=" ")

The print function prints something, in this case the generated number, but itself has no return value. So b will be None.

a.append(b)

This could theoretically append the generated number to the list. However, as we established, b is None, so this will append None to the list. Furthermore, this line is outside of the loop, so this will only run once, and would only add a single of your 100 numbers.

You should do something like this instead:

a = []
for x in range(100):
    num = random.randint(1,501)
    a.append(num)

print(a)
print(len(a))

You can also use a list comprehension here, to make it simpler:

a = [random.randint(1, 501) for x in range(100)]
print(a)
print(len(a))

This will have the same result.

You could also use random.choices which on its own is already capable of generating a list of random numbers with a specified size. So this would work just as well:

a = random.choices(range(1, 501), k=100)

Upvotes: 3

Related Questions