随机数的产生
在网上搜索了N久,都是些大同小异的实现方法.要么有些根本就不行.请各位来说说,究竟如何才能产生比较满意的随机数.
前提是: 不用与rand()类似的一些库函数.
// crt_rand.c // This program seeds the random-number generator // with the time, then displays 10 random integers. // #include <stdlib.h> #include <stdio.h> #include <time.h> int main( void ) { int i; // Seed the random-number generator with current time so that // the numbers will be different every time we run. // srand( (unsigned)time( NULL ) ); // Display 10 numbers. for( i = 0; i < 10;i++ ) printf( " %6d\n", rand() ); printf("\n"); // Usually, you will want to generate a number in a specific range, // such as 0 to 100, like this: { int RANGE_MIN = 0; int RANGE_MAX = 100; for (i = 0; i < 10; i++ ) { int rand100 = (((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN); printf( " %6d\n", rand100); } } getchar(); return 0; }