Chinaunix首页 | 论坛 | 博客
  • 博客访问: 225300
  • 博文数量: 36
  • 博客积分: 482
  • 博客等级: 下士
  • 技术积分: 290
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-03 12:14
个人简介

Hi-ho, Silver! 在一个商业英雄辈出的年代,让我们用技术做一次华丽的冒险。向Linus致敬,向Stallman致敬!

文章分类

全部博文(36)

文章存档

2013年(24)

2012年(1)

2011年(8)

2010年(3)

我的朋友

分类: C/C++

2013-03-07 11:17:32

c的随机数函数rand_r()

推荐用rand_r()来产生随机数。

顺便普及一下很多地球人都知道的常识

大家都知道随机种子(rand seed),所谓的随机其实是:每一个种子会有一串看似随机的序列,每次取下一个出来,整体都近乎是随机分布的。换句话说,每一次改变随机种子变量的值,这个随机数都会重新开始。这样其实能带来一个副产品般的好处,结果可以重现,便于测试。


1. 最简单的例子

见随机代码如下,每一次更改值后,都会重新随机序列。


  1. #include   
  2. #include   
  3.   
  4. using namespace std;  
  5.   
  6. int main() {  
  7.   unsigned int seed = 123;  
  8.   cout << rand_r(&seed) << endl;  // 2067261                                                                                                                  
  9.   cout << rand_r(&seed) << endl;  // 384717275                                                                                                                
  10.   seed = 456;  
  11.   cout << rand_r(&seed) << endl;  // 7663992                                                                                                                  
  12.   cout << rand_r(&seed) << endl;  // 2107178371                                                                                                               
  13.   seed = 123;  
  14.   cout << rand_r(&seed) << endl;  // 2067261                                                                                                                  
  15.   cout << rand_r(&seed) << endl;  // 384717275                                                                                                                
  16.   seed = 456;  
  17.   cout << rand_r(&seed) << endl;  // 7663992                                                                                                                  
  18.   cout << rand_r(&seed) << endl;  // 2107178371                                                                                                               
  19. }  

2. 如果做成函数,不能是局部变量

下面代码表示,要注意,rand_r() 只关心变量本身,如果换成一个局部变量,就不行了,比如这样,随机值就不会变了。


  1. #include   
  2. #include   
  3.   
  4. using namespace std;  
  5.   
  6. unsigned int seed_ = 123;  
  7.   
  8. unsigned int GetRand() {  
  9.   unsigned int seed = seed_;  
  10.   return rand_r(&seed);  
  11. }  
  12.   
  13. int main() {  
  14.   seed_ = 123;  
  15.   cout << GetRand() << endl;  // 2067261                                                                                                                      
  16.   cout << GetRand() << endl;  // 2067261                                                                                                                    
  17.   seed_ = 456;  
  18.   cout << GetRand() << endl;  // 2067261                                                                                                                      
  19.   cout << GetRand() << endl;  // 2067261                                                                                                                   
  20.   seed_ = 123;  
  21.   cout << GetRand() << endl;  // 2067261                                                                                                                      
  22.   cout << GetRand() << endl;  // 2067261                                                                                                                    
  23.   seed_ = 456;  
  24.   cout << GetRand() << endl;  // 2067261                                                                                                                      
  25.   cout << GetRand() << endl;  // 2067261                                                                                                                   
  26. }  


3. 类函数

唯一不爽的是,rand_r()不能用于const的类函数,因为如果seed_作为成员变量的话,rand_r()要改变成员变量,真的要实现,除非seed_作为static的全局变量。见代码如下:

但要考虑线程安全问题了(本代码没包含)。



  1. using namespace std;  
  2.   
  3. class R {  
  4. public:  
  5.   R() { }  
  6.   unsigned int GetRand() const {  
  7.     return rand_r(&seed_);  
  8.   }  
  9.   void set_seed(const unsigned int seed) {  
  10.     seed_ = seed;  
  11.   }  
  12. private:  
  13.   static unsigned int seed_;  
  14. };  
  15.   
  16. unsigned int R::seed_ = 0;  
  17.   
  18. int main() {  
  19.   R r;  
  20.   r.set_seed(123);  
  21.   cout << r.GetRand() << endl;  // 2067261                                                                                                                    
  22.   cout << r.GetRand() << endl;  // 384717275                                                                                                                  
  23.   r.set_seed(456);  
  24.   cout << r.GetRand() << endl;  // 7663992                                                                                                                    
  25.   cout << r.GetRand() << endl;  // 2107178371                                                                                                                 
  26.   r.set_seed(123);  
  27.   cout << r.GetRand() << endl;  // 2067261                                                                                                                    
  28.   cout << r.GetRand() << endl;  // 384717275                                                                                                                  
  29.   r.set_seed(456);  
  30.   cout << r.GetRand() << endl;  // 7663992                                                                                                                    
  31.   cout << r.GetRand() << endl;  // 2107178371                                                                                                                 
  32. }  

转自: http://blog.csdn.net/made_in_chn/article/details/6604500


阅读(17293) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~