Thread: Normal Distribution Histogram

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    5

    Normal Distribution Histogram

    Hey guys, I'm stuck on the beginning of this assignment. I've been googling it for hours and re watching the lecture, but I can't figure it out.

    PROBLEM: Write a function that generates 1000 normally distibuted (Gaussian Probabliity Distribution) random numbers. Range should be between -3 and +3. Numbers should be double floating point.

    There's more to it than that, but I've got it from there.

    Thanks!
    Last edited by Michael Rupert; 02-06-2013 at 06:27 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Michael, welcome to the forum

    First of all, in parallel some other people asked about Random numbers ∈[min, max].
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    @std10093
    That will only give him numbers that are more or less uniformly distributed. He needs a normal (aka Gaussian) distribution.

    @Michael Rupert
    You didn't specify a standard deviation for your distribution, so I assume it will be 1.
    Check this out: Normal distribution - Wikipedia, the free encyclopedia. The second method listed, based on the central limit theorem, is pretty easy to implement. Note that it gives you values from -6 to +6. You can find various ways to scale that to your needs of [-3, 3]. Note, the more numbers you sum up, the more accurate your distribution will be, so long as you scale it down correctly, to fit your +/-3 range.

    Note, I'm not a math expert, so I can't really speak to how scaling that method up/down will affect the overall accuracy/precision of your results, nor how to adapt that to a standard deviation other than 1.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Quote Originally Posted by anduril462 View Post
    @Michael Rupert
    You didn't specify a standard deviation for your distribution, so I assume it will be 1.
    Check this out: Normal distribution - Wikipedia, the free encyclopedia. The second method listed, based on the central limit theorem, is pretty easy to implement. Note that it gives you values from -6 to +6. You can find various ways to scale that to your needs of [-3, 3]. Note, the more numbers you sum up, the more accurate your distribution will be, so long as you scale it down correctly, to fit your +/-3 range.

    Note, I'm not a math expert, so I can't really speak to how scaling that method up/down will affect the overall accuracy/precision of your results, nor how to adapt that to a standard deviation other than 1.
    As a new C user, I honestly have no idea how to even start to implement that, could anyone give me a hand?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Just how new are you? Honestly, if you can't even start to implement that, then the level required for this assignment does not seem to jive with your abilities. From a basic programming standpoint: Can you generate 12 random numbers? Can you subtract one number from another? From a general problem solving standpoint: can you implement that method by hand? If not, you stand no chance of writing a program to do so.

    Perhaps you could provide links to the actual homework assignment, and the video lecture(s). Can't say I'll watch the whole thing, but it might give an idea of what level the teacher expects you to be at. Maybe there are some clues in the assignment you're missing.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    PROBLEM: Write a function that generates 1000 normally distributed (Gaussian Probabliity Distribution) random numbers. Range should be between -3 and +3. Numbers should be double floating point. And different random numbers should come out every time it runs. Use the putpixel function (graphics.h), or the bar function (graphics.h) plot a histogram of the numbers you generated. Try to have somewhere between 20 and 40 bins. You will know you have the correct answer when the histogram has a somewhat bell curve shape.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There are still so many questions.

    • Is that the complete problem description, copied verbatim?
    • What is your background in C? What about programming in general? Matlab?
    • You must understand the problem before you can program a solution.
    • Have you ever been taught what a normal/Gaussian distribution is? What is your background in statistics?
    • Do you know what a histogram is? Can you create a histogram on paper with, say, 10 bins, and fill it with a uniform distribution of 50 random numbers? Google "online random number generator" to get some random data for your paper histogram.
    • Can you show us any attempt to solve this problem yourself? Have you tried writing any code at all? Note, we don't hand out free homework here, we help you learn. That requires some show of effort, of good faith, on your part.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This part sounds doable:
    An easy to program approximate approach, that relies on the central limit theorem, is as follows: generate 12 uniform U(0,1) deviates, add them all up, and subtract 6 – the resulting random variable will have approximately standard normal distribution. In truth, the distribution will be Irwin–Hall, which is a 12-section eleventh-order polynomial approximation to the normal distribution. This random deviate will have a limited range of (−6, 6).[39]
    The uniform numbers between 0 and 1 could be from rand() I believe. You're only interested in -3 3, but should be able to remove/ignore the one's out of range, yes?

    Try Googling central limit theorem in C language and see what's out there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Histogram
    By SpecKROELLchen in forum C Programming
    Replies: 9
    Last Post: 08-02-2012, 03:10 AM
  2. Visual C++ Log Normal Distribution
    By juyew in forum C++ Programming
    Replies: 5
    Last Post: 03-18-2012, 10:03 PM
  3. Generate numbers according to Normal Distribution
    By anirban in forum C Programming
    Replies: 1
    Last Post: 11-27-2010, 08:53 AM
  4. Normal maps: Get normal x/y/z from color
    By Devils Child in forum Game Programming
    Replies: 2
    Last Post: 08-09-2009, 12:01 PM
  5. standard Normal Distribution
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 05-21-2002, 11:45 AM