Chris Formosa
Chris Formosa

Reputation: 3

Trying to create a random list

I am fairly new to programming and I am having trouble figuring out this equation. I need to create a list from 100 integers that are generated at the range from 50 to 99.

From this I must create a steam and leaf plot, but I am having trouble with the random list. Must I use xrange or randomint? Any suggestions would help!

Upvotes: 0

Views: 90

Answers (3)

Alexander
Alexander

Reputation: 109726

If you can use Numpy:

import numpy as np

>>> np.random.randint(low=50, high=100, size=100).tolist()
[69, 93, 87, 53, 84, 54, 79, 98, 82, 86, 58, 58, 68, 62, 55, 55, 90,
 98, 89, 63, 99, 64, 78, 84, 61, 58, 85, 64, 52, 79, 90, 72, 58, 53,
 81, 71, 78, 90, 78, 78, 82, 61, 65, 72, 60, 61, 52, 85, 77, 62, 70,
 70, 92, 80, 76, 92, 59, 55, 65, 92, 64, 53, 69, 69, 90, 99, 86, 51,
 81, 67, 67, 58, 60, 85, 71, 59, 50, 60, 66, 87, 98, 75, 96, 72, 56,
 85, 68, 81, 74, 72, 95, 71, 57, 59, 71, 73, 88, 79, 65, 93]

Note that the high number is excluded, i.e. from and including low to but excluding high.

Upvotes: 1

Malik Brahimi
Malik Brahimi

Reputation: 16721

You need both randint and range in your list comprehension to create 100 random numbers:

data = [randint(50, 99) for i in range(100)]

Upvotes: 5

Zizouz212
Zizouz212

Reputation: 4998

Here is another way that is more straightforward: (mind you, list comprehensions are more pythonic)

import random
mylist = []
for i in range(100):
    mylist.append(random.randint(50,99))

Upvotes: 1

Related Questions