文章主要参考http://blog.csdn.net/jimmyblind/article/details/5550042
srand48和drand48是Unix库函数,drand48的作用是产生[0,1]之间均匀分布的随机数,采用了线性同余法和48位整数运算来产生伪随机序列
![image image](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
其中
函数用上面的算法产生一个48位的伪随机整数,然后再取出此整数的高32位作为随机数,然后将这个32位的伪随机数规划到[0,1]之间,用函数srand48来初始化drand48(),其只对于48位整数的高32位进行初始化,而其低16位被设定为随机值。
1: #define MNWZ 0x100000000
2: #define ANWZ 0x5DEECE66D
3: #define CNWZ 0xB16
4: #define INFINITY 0xFFFFFFFFF
5:
6: int labelsize;
7: int dim;
8:
9: static unsigned long long seed = 1;
10:
11: double drand48(void)
12: {
13: seed = (ANWZ * seed + CNWZ) & 0xFFFFFFFFFFFFLL;
14: unsigned int x = seed >> 16;
15: return ((double)x / (double)MNWZ);
16: }
17:
18: //static unsigned long long seed = 1;
19:
20: void srand48(unsigned int i)
21: {
22: seed = (((long long int)i) << 16) | rand();
23: }