Another random number generator C code example using rand() function
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: none
To do: Another random number generator C program example using rand() function
To show: How to use C rand() function to generate a random number
// Using rand() function from stdlib.h
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int c, num, num1;
double num3;
// generating random number
printf("10 integers random numbers:\n");
// loop
for(c = 0; c < 10; c++)
printf("%d ", rand());
printf("\n");
// num has a value between 0 and 99
num = rand() /100;
printf("\nRandom number less than 100 = %d\n", num);
// using lower bound and upper bound
num1 = 200 + (rand()/(700 - 200));
printf("\nRandom number between 200 and 700 = %d\n", num1);
// floating-point random numbers...
num3 = rand()/33000.0;
printf("\nfloating-point random number = %f\n", num3);
// floating-point random numbers...
printf("\nAnother floating-point random numbers:\n");
for(c = 0; c <10; c++)
printf("%f\n", rand()/33000.0);
printf("\n");
return 0;
}
Output example:
10 integers random numbers:
41 18467 6334 26500 19169 15724 11478 29358 26962 24464
Random number less than 100 = 57
Random number between 200 and 700 = 256
floating-point random number = 0.705485
Another floating-point random numbers:
0.509909
0.301848
0.014879
0.090758
0.361879
0.146273
0.164727
0.981545
0.442545
0.118242
Press any key to continue . . .