//生成[a,b]之间的随机整数
int Random (int a, int b)
{
int area=0;
int ret=0;
//生成区间
area=b-a+1;
//生成随机数
ret=(int)(rand()*area/(1.0 * RAND_MAX)+a);
return ret;
}
▄▄▄▄
▌rand▐ <STDLIB.H>
▀▀▀▀▀▀
Random number generator
Declaration: int rand(void);
Remarks:
rand uses a multiplicative congruential random number generator with period
232 to return successive pseudo-random numbers in the range 0 to RAND_MAX.
Return Value:
rand returns the generated pseudo-random number.
Portability:
╔ DOS ╤ UNIX ╤ ANSI C ╤ C++ Only ╗
║ Yes │ Yes │ Yes │ ║
╚══╧═══╧════╧═════╝
See Also:
random randomize srand
Example:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int i;
printf("Ten random numbers from 0 to 99\n\n");
for(i=0; i<10; i++)
printf("%d\n", rand() % 100);
return 0;
}