c++rand()与srand()的使用

一、随机函数rand()

函数原型:int  rand(void)

所在头文件:stdlib.h

功能:生成随机数

初始化随机函数srand()

函数原型:void  srand(unsigned seed)

所在头文件:stdlib.h

功能:产生seed,用于初始化rand()的起始值

time()函数

函数原型:time_t  time(time_t  *timer)

所在头文件:time.h

功能:返回一个从January  1,1970到当前时刻所经历的时间

二、使用说明

1.srand()函数的主要作用是生成rand()函数的种子,其参数seed就是种子,用来初始化rand()的起始值;

2.rand函数生成数并不是真正的随机数,而是伪随机数,当用户未设定随机数种子时,系统默认随机数种子是1,此时程序每次产生的随机数相同;

3.rand()函数在执行前,总是先去检查srand()函数设置的种子;若用户未设定,则默认调用种子为1;若设置了种子seed,则调用srand(seed)来初始化它的起始值;

4.可以得出结论,若每次设置的种子相同,或者未设置种子(默认种子为1),那么产生的随机数也相同。这时可以调用一个time()函数,随着时间的改变使程序每次运行时生成不同的seed,从而使每次的随机数列也不同。

三、使用方法

1.未设置seed

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
   for(int i=0;i<10;i++)
   cout<<rand()<<" ";
   return 0;
}
2.用time函数设置seed

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
   srand((unsigned)time(0));
   for(int i=0;i<10;i++)
   cout<<rand()<<" ";
   return 0;
}
3.生成一定范围内的数,a+rand()%n;其中a是范围的起始值,n是从起始值开始数的个数,n+a-1即为最大值。

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
   srand((unsigned)time(0));
   for(int i=0;i<10;i++)
   cout<<15+rand()%5<<" ";
   return 0;
}
简单的四六级计分程序,其中a数组存放的是标准答案,随机生成;b数组存放考生答案,也是随机生成。统计每份试卷的正确率和分数。

#include<iostream>
#include<string>
#include<stdlib.h>
#include<time.h>
#define MAXSIZE 55///选择题的数量
#define MAXSIZE2 60///卷子的数量
using namespace std;
int a[MAXSIZE][MAXSIZE];
int count;///计数
int main()
{
    cout<<"  * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl;
    cout<<"  *  试卷说明:                                   *"<<endl;
    cout<<"  *  共65道选择题,其中1~25为听力,每题7.1分;    *"<<endl;
    cout<<"  *  26~35为完型填空,每题3.55分;                *"<<endl;
    cout<<"  *  36~45为快速匹配,每题7.1分;                 *"<<endl;
    cout<<"  *  46~55为阅读,每题14.2分;                    *"<<endl;
    cout<<"  * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl;
    int i,k,score;
    char a[MAXSIZE+1],b[MAXSIZE+1];
    count=0;k=1;
    srand((unsigned)time(0));///时间不同,产生的种子也不同

    ///生成随机标准答案
    for(i=0;i<55;i++)
    {
        if(i>=35&&i<=44)
           a[i]=rand()%12+65;
        else
            a[i]=rand()%4+65;
    }
    for(i=0;i<55;i++)
    {
       cout<<a[i]<<" ";
       if((i+1)%11==0)
            cout<<endl;
    }
    cout<<endl;
     while(k<=60)
     {
         score=0;
         for(i=0;i<55;i++)
       {
          if(i>=35&&i<=44)
            b[i]=rand()%12+65;
          else
            b[i]=rand()%4+65;
       if(b[i]==a[i])
        {
           count++;
           if(i>=25&&i<=34)
            score+=3.55;
            else if(i>=45)
                score+=14.2;
            else
                score+=7.1;
        }
       }
     for(i=0;i<55;i++)
       {
         cout<<b[i]<<" ";
         if((i+1)%11==0)
            cout<<endl;
       }
    cout<<"第"<<k++<<"套卷子的正确率为:"<<(float)count/55<<"%"<<endl;
    cout<<"分值为:"<<score<<endl;
    cout<<endl;
     }
   system("pause");
   return 0;
}


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值