标题:关于rand()随即函数的使用
取消只看楼主
witchery
Rank: 1
来 自:西安
等 级:新手上路
帖 子:205
专家分:0
注 册:2005-8-6
 问题点数:0 回复次数:2 
关于rand()随即函数的使用
在使用rand()产生随即数时经常会发现每次产生的数值总是一样。
那时因为用rand()产生的是伪随即数,伪随即数的产生需要用 随即数种子来控制 种子相同伪随即数的排列顺序不便,而且当不对种子进行设置其默认制为 1 . 所以每次产生的随即数总是一样。
    伪随即数是用srand(unsigned int )函数产生。
   e.g:
         #include<iostream>
          using namespace std;
          main()
         {
           int seed ;
           cin>>seed;        //随即数种子输入
           srand(seed);
           cout<<rand();
          }
  如有错误和补充  恳请高手指点  

           
        
搜索更多相关主题的帖子: rand 函数 
2005-08-07 11:39
witchery
Rank: 1
来 自:西安
等 级:新手上路
帖 子:205
专家分:0
注 册:2005-8-6
得分:0 
srand() 函数中的参数用时间就行了,每一时刻的时间总是不一样的,那seed 就不可能保持一致。
2005-08-14 12:57
witchery
Rank: 1
来 自:西安
等 级:新手上路
帖 子:205
专家分:0
注 册:2005-8-6
得分:0 

/* RAND.C: This program seeds the random-number generator * with the time, then displays 10 random integers. */#include <stdlib.h>#include <stdio.h>#include <time.h>void main( void ){   int i;   /* Seed the random-number generator with current time so that    * the numbers will be different every time we run.    */   srand( (unsigned)time( NULL ) );   /* Display 10 numbers. */   for( i = 0;   i < 10;i++ )      printf( "  %6d\n", rand() );}

Output

    6929    8026   21987   30734   20587    6699   22034   25051    7988   10104
2005-08-29 21:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-24567-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.435773 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved