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

1. ## 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. 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. How do I write code for 'if a is between b and b+1'?

4. 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.

5. Originally Posted by meadmead
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