[转]windows下srand48()和drand48()的问题

转自:windows下srand48()和drand48()的问题


srand48和drand48的原理实现


 

 1 #ifndef DRAND48_H
 2 #define DRAND48_H
 3 
 4 #include <stdlib.h>
 5 
 6 #define m 0x100000000LL
 7 #define c 0xB16
 8 #define a 0x5DEECE66DLL
 9 
10 static unsigned long long seed = 1;
11 
12 double drand48(void)
13 {
14     seed = (a * seed + c) & 0xFFFFFFFFFFFFLL;
15     unsigned int x = seed >> 16;
16     return     ((double)x / (double)m);
17     
18 }
19 
20 void srand48(unsigned int i)
21 {
22     seed  = (((long long int)i) << 16) | rand();
23 }
24 
25 #endif

 

 

posted @ 2013-08-25 23:02  Teilwall  阅读(1230)  评论(1编辑  收藏  举报