rand.h
#ifndef RAND_H
unsigned long int next = 1;
int rand() {
next = next * 1103515245 + 12345;
return (unsigned int)(next/65535) % 32768;
}
void seed(unsigned int seed) {
next = seed;
}
#endif
main.c
#include <stdio.h>
#include "rand.h"
int main(int argc, char **argv) {
int num;
while (1) {
printf("Please enter the seed\n");
scanf("%d", &num);
seed(num);
printf("The value is %d\n", rand());
}
return 0;
}
使用%取模可以用于产生任意整形范围内的随机数。