比較好的 亂數
(高顯忠, sjgau4311@gmail.com, 2010-12-22 11:23)
1樓
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
// ----------------------------------------------
using namespace std;
// ----------------------------------------------
#if 0
#endif
// ----------------------------------------------
int rand1(void)
{
int r1, r2;
r1= rand();
r2= rand();
return((r1<<15) + r2);
}// end of rnd1()
// ----------------------------------------------
void init_rand(void)
{
int t1, t2, ct1, i;
ct1= 0;
t1= time(NULL);
t2= t1;
while (t1 == t2) {
ct1++;
t1= time(NULL);
}
// get a new t1
srand((unsigned) t1);
for (i=0;i<300;i++) {
rand();
}
}// end of init_rand()
// ----------------------------------------------
int main(int argc, char *argv[])
{
int i;
time_t t1;
for (;;) {
// srand((unsigned) time(&t1) );
// printf("t1= %d, year= %.3lf\n\n", t1, ((((double) t1)/86400.0/365.25) + 1970));
init_rand();
for( i = 0; i < 10;i++ ) {
printf( " %6d\n", rand1()%100);
}
system("PAUSE");
}
return EXIT_SUCCESS;
}// end of main()
// ----------------------------------------------