分类: LINUX
2006-10-19 15:27:18
|
[zhanghua@melon TEST]$ gcc rand_r-test.c -o rand_r-test
[zhanghua@melon TEST]$ ./rand_r-test
rand = 0.4
rand = 0.1
rand = 0.4
rand = 0.3
rand = 0.3
rand = 0.3
rand = 0.2
rand = 0.4
rand = 0.2
rand = 0.4
[zhanghua@melon TEST]$ ./rand_r-test
rand = 0.2
rand = 0.3
rand = 0.1
rand = 0.2
rand = 0.3
rand = 0.5
rand = 0.2
rand = 0.1
rand = 0.2
rand = 0.2
上面例子生成0.1 - 0.5之间的随机数.
其中:
rand_r(&seed)/RAND_MAX将得到0 -1 之间的数.
乘以0.4则生成0 - 0.4之间的数, 再加上0.1得到0.1 - 0.5之间的随机数.