# Random Number Help

This is a discussion on Random Number Help within the C Programming forums, part of the General Programming Boards category; Hello! I'm new to the forum and I just wanted to say hello and get some assistance. I'm currently teaching ...

1. ## Random Number Help

Hello! I'm new to the forum and I just wanted to say hello and get some assistance.

I'm currently teaching myself the C language before starting the class in the fall. I'm in the process of writing a program that prints out 200 random numbers and at the same time count the number of rundom numbers that is printed below RAND_MAX/2.

Printing out the 200 random numbers was no problem but when it came to counting the random numbers that was below the RAND_MAX/2. It didn't work out as well as I hoped. Can somebody give me some assistance on this. Below is what I came up with.

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
int i,n;
int median = RAND_MAX/2;
int plus_cnt = 0;
int sum = 0;

for (i=0; i<10; ++i){

if (i%6 == 0)
printf("\n");
printf("%10d", rand());

if( rand()< median)
sum = sum + 1;
plus_cnt = plus_cnt + 1;

}

printf("\n");
printf("sum = %d\n", sum);
return 0;
}

2. every time you call the rand() function its going to return a different random number, so first set the return of rand() to variable and reference that instead.

also you can simplify these statements:
sum = sum + 1;
plus_cnt = plus_cnt + 1;

C has an operator for this frequently done task: ++

++sum; is the equivilent to doing sum = sum + 1;

you should look it up, as using it on either side of the variable (post inc and the other one i forget the name..heh) have different meanings.

one last thing, next time use code tags.

3. ++sum; is the equivilent to doing sum = sum + 1;
Close but not 100% identical.

When ++sum or sum++ are on their own line it is but if you combine it with another statement such as: printf("%d", sum++) or printf("%d", ++sum) the difference becomes a big deal. I won't give you the answer but lookup pre and post increment. A better subsitution is sum += 1;

It didn't work out as well as I hoped. Can somebody give me some assistance on this.
How do the results you expect differ from the results you got?

Also please read the stick thread at the top of forum to learn how to use the code tags, it aids greatly in readibility of the code on the forum.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
int i, median = RAND_MAX / 2, sum = 0;
printf("median = %d\n", median);
srand(time(0));
for ( i = 0; i < 20; ++i )
{
int result = rand();
printf("%10d", result);
if ( i % 6 == 5 )
{
putchar('\n');
}
if ( result < median )
{
++sum;
}
}
printf("\nsum = %d\n", sum);
return 0;
}

/* my output
median = 16383
995      9630     15624      7358     13419     30050
17016     14786     16103     17981     13452      4755
26854      4556     26678     20163     18700     25290
2696     24808
sum = 11
*/```

5. I believe i stated that just below what you quoted...more or less. But whatever.

6. Thank you, all your suggestions and example definitely helped me understand how to work the problem. Next time I'll use the code tags.