469,129 Members | 1,735 Online

Hello everyone,

I am trying to make a histogram for a project in my class and I really need some help. Here is the question that my instructor provided:

Create 1000 Random numbers between 0-100. Create a histogram of the values as a list. Make the bin range 10. Your book goes through a more complicated, but similar exercises.

Here is the code that I have so far:

import random
list = []
count = 0

for i in range(1000):
i = random.randrange(0, 101, 1)
print i
list = list + [i]
count = count + 1

print 'The total number of random numbers is: \t', count

Even with the help of my book I am unable to make the histogram.....can someone PLEASE help me...its driving me crazy.
Jun 24 '08 #1
1 4471
Hi,

Not really sure from you post what type of random number generator that you
need to use but just continuing on from what you were doing in your post with
'random.randrange' ... I put together this bit of code. You must have the
extra python packages 'scipy' and 'Gnuplot.py' installed ... depending on your
system?? you should be able to install these easily. Otherwise I guess you
can calculate you histogram bins yourself (should only be a few lines of code)
and you can always plot the histogram in an external package like Gnuplot
which is standard on most *nix machines.

Hope this helps you ...

Expand|Select|Wrap|Line Numbers
1.
2. import random
3. import Gnuplot
4. import scipy
5.
6. # Generate a list of random numbers
7.
8. hist=[]
9. i=0
10. while i < 1000:
11.     item = random.randrange(0,101,1)
12.     hist.append(item)
13.     i+=1
14.
15. # Generate your histogram
16.
17. (list,bins) = scipy.histogram(hist,bins=10)
18.
19.
20. # Plot your histogram
21.
22. plot = Gnuplot.Gnuplot(persist=1)
23. plot('set terminal x11 1 enhanced')
24. plot.xlabel("No. of Bins")
25. plot.ylabel("Count")
26. dat = Gnuplot.Data(bins,list, with="histeps")
27. plot.plot(dat)
28.
29.
Jun 25 '08 #2