C examples for stdlib.h:rand
function
<cstdlib> <stdlib.h>
Generate random number in the range between 0 and RAND_MAX.
int rand (void); The following code shows how to create random number in a certain range.
v1 = rand() % 100; // v1 in the range 0 to 99 v2 = rand() % 100 + 1; // v2 in the range 1 to 100 v3 = rand() % 30 + 1985; // v3 in the range 1985-2014
none
An integer value between 0 and RAND_MAX.
#pragma warning(disable:4996)//from www . j a v a2s . co m #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int iSecret, iGuess; /* generate secret number between 1 and 10: */ iSecret = rand() % 10 + 1; do { printf("Guess the number (1 to 10): "); scanf("%d", &iGuess); if (iSecret<iGuess) puts("The secret number is lower"); else if (iSecret>iGuess) puts("The secret number is higher"); } while (iSecret != iGuess); return 0; }