469,129 Members | 1,735 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,129 developers. It's quick & easy.

Histogram-Please Help

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

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
  2. import random
  3. import Gnuplot
  4. import scipy
  6. # Generate a list of random numbers
  8. hist=[]
  9. i=0
  10. while i < 1000:
  11.     item = random.randrange(0,101,1)
  12.     hist.append(item)
  13.     i+=1
  15. # Generate your histogram
  17. (list,bins) = scipy.histogram(hist,bins=10)
  20. # Plot your histogram
  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)
Jun 25 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Oracle3001 | last post: by
1 post views Thread by bleh | last post: by
27 posts views Thread by ext_u | last post: by
5 posts views Thread by firewoodtim | last post: by
12 posts views Thread by KraftDiner | last post: by
5 posts views Thread by arnuld | last post: by
reply views Thread by Kurt Smith | last post: by
15 posts views Thread by zaturn | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.