Thread: Sorting and Binning Random Numbers Problem....

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    9

    Unhappy Sorting and Binning Random Numbers Problem....

    Im a complete novice to C programming and have had a problem sheet given to me for the start of my second year physics degree. The question is this;

    Write a program to calculate the distribution of numbers produced by the random number function rand().

    1) Calculate a random number between 0 and 1. To do this use rand() and divide by RAND_MAX.

    2) Next write some code to ‘bin’ the data. Create an array where each element represents a bin, (use, say, 10 bins to start). Increment (increase by one) the array element where the number falls. You need to calculate which bin the random number will fall into. If x is the random number between 0 and 1 and there are 10 bins, then the bin number will be the integer part of x*10 (assuming your array starts at 0).
    Repeat this for a large number of times (use a for loop).
    Calculate the normalised frequency of occurrence in each bin (just the number of bin elements divided by the total number of samples). Then display the results.

    This is my code;

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {    
        int a,b,bin[10];    
        for (b=0; b<30; b=b+1)    
        {
            a=10*rand()/(double)RAND_MAX;        
            if (a>b)             
                {
                    if (a<b+1) 
                        {
                            bin[b]+=1;
                        }
                }            
        }
        for (b=0; b<10; b=b+1)
        {
            printf("Count number in bin %d is %d\n", b,bin[b]);
        }    
        return 0;    
    }
    When I execute it, I get this;
    Count number in bin 0 is 0
    Count number in bin 1 is 0
    Count number in bin 2 is 0
    Count number in bin 3 is 0
    Count number in bin 4 is 0
    Count number in bin 5 is 0
    Count number in bin 6 is 0
    Count number in bin 7 is 0
    Count number in bin 8 is 0
    Count number in bin 9 is 0

    which is obviously not what I want. I have been looking at this and fiddling with it for hours and hours, where have I gone wrong?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    quick guess

    Code:
    int a,b,bin[10];
    should be

    Code:
    int a,b,bin[10]={0};
    You need to set bin to all zeros.

    How often will "a" be both greater than "b" and less than "b+1" at the same time. I think never.
    Code:
    if (a>b)             
                {
                    if (a<b+1)
    Tim S.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    9
    How do I write code for 'if a is between b and b+1'?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
        for (b=0; b<30; b=b+1)   /* why to 30? */
        {
            a=10*rand()/(double)RAND_MAX;       /* why not just rand() % 10 */
            if (a>b) /* why? */
                {
                    if (a<b+1)
                        {
                            bin[b]+=1;
                        }
                }           
        }
    The majority of those loops aren't going to do anything. I don't know why you are going to 30 when nothing past b[9] is valid. You can't ever fill anything past b = 9, because if you did, you'd run off the end of your array. Also, b = 0 would always be empty too, because since a can't be negative, and since b is an integer, a will never be less than b when it is zero.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by meadmead View Post
    How do I write code for 'if a is between b and b+1'?

    You don't.

    In your code sample a and b are integers, whole numbers... no decimal paces...
    So working only with whole numbers lets say b is 12 ... b+1 has to be 13 ...
    How many whole numbers are there between 12 and 13?

    Really...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting random numbers using pointing arrays.
    By jFran in forum C Programming
    Replies: 8
    Last Post: 05-10-2011, 01:56 PM
  2. Problem with random numbers.
    By oror84 in forum C Programming
    Replies: 6
    Last Post: 05-01-2011, 01:22 PM
  3. problem with random sorting
    By Exorcist in forum C Programming
    Replies: 5
    Last Post: 12-03-2009, 01:51 AM
  4. problem with random numbers
    By xxwerdxx in forum C Programming
    Replies: 2
    Last Post: 11-24-2005, 08:56 AM
  5. Sorting Random Numbers
    By kid kash in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2002, 04:47 AM