第五周:Random随机数的练习

目录

一、 初步了解

1.  setw(int n)

2. rand( )

3.程序1:

二、掷骰子

三、srand(unsigned int seed)函数

1. rand辨析

2.srand(unsigned int seed)

3. 程序2:

四、掷骰子:使用srand(time(0))和枚举类型

1. time()

2. 枚举类型

3. 程序3


 

一、 初步了解

1.  setw(int n)

头文件:#include<iomanip>

占位函数,处理输出间隔的问题,占n个位,不足的用空格补充

2. rand( )

头文件:#include<cstdlib>

rand( ) %n:产生【0,n-1】 的随机数;

rand( ) %n+1:产生【1,n】的随机数;

 

3.程序1:

#include <iostream>
#include<iomanip>  //cintains the fuction prototype for setw
#include<cstdlib> //contains the fuction prototype for rand
using namespace std;

int main(){
    for(int i =1;i<=20;i++){
        cout << setw(10) << (rand()%6+1);

        if(i%5==0) cout <<endl;
    }

}

         6         6         5         5         6
         5         1         1         5         3
         6         6         2         4         2
         6         2         3         4         1

 

 

二、掷骰子

#include <iostream>
#include <cstdlib>
#include<iomanip>
using namespace std;

int main()
{
    int f1=0; //每面出现的次数
    int f2=0;
    int f3=0;
    int f4=0;
    int f5=0;
    int f6=0;
    for(int roll=1;roll<=60;roll++){  //掷骰子60次
        int face=1+rand()%6;  //记录每次掷骰子掷导的面数
        switch(face){
        case 1:
            f1++;break;

        case 2:
            f2++;break;

        case 3:
            f3++;break;

        case 4:
            f4++;break;

        case 5:
            f5++;break;

        case 6:
            f6++;break;

        default:cout <<"Program should never get here!"<<endl;
        }
    }
        cout <<"Face"<<setw(13)<<"Frequency"<<endl;
        cout <<"   1"<<setw(13)<<f1;
      cout <<"\n   2"<<setw(13)<<f2;
      cout <<"\n   3"<<setw(13)<<f3;
      cout <<"\n   4"<<setw(13)<<f4;
      cout <<"\n   5"<<setw(13)<<f5;
      cout <<"\n   6"<<setw(13)<<f6;

    return 0;
}

 

 

三、srand(unsigned int seed)函数

 

1. rand辨析

rand()产生的是伪随机数:有规律可循的随机数,程序每次调用都会重复执行刚才的产生数序列

函数在调用时,自动设置种子数seed=1;种子数相同,产生的随机数序列也相同

 

2.srand(unsigned int seed)

srand(int seed) :设置rand函数的种子值,产生真正的、无逻辑可循的随机数

头文件:#include<cstdlib>

srand()用来设置rand()产生随机数时的随机数种子。参数seed是整数,通常可以利用time(0)或geypid(0)的返回值作为seed。

 

3. 程序2:

在一的基础上用srand设置了seed值 

#include <iostream>
#include <cstdlib>
#include<iomanip>
using namespace std;

int main()
{
    unsigned seed;
    cout<<"Enter seed"<<endl;
    cin>>seed;
    srand(seed);

    //与程序一相同,设置了seed值后,每次运行产生的随机数都不一样

    for(int i =1;i<=20;i++){
        cout << setw(10) << (rand()%6+1);

        if(i%5==0) cout <<endl;
    }
    return 0;
}

 

 

四、掷骰子:使用srand(time(0))和枚举类型

1. time()

#include<ctime>

2. 枚举类型

enum 枚举类型{常量列表}

e.g. enum color{red,yellow,blue,purple,green}

 

3. 程序3

#include <iostream>
#include <cstdlib>
#include<iomanip>
#include<ctime>  //time的头文件
using namespace std;

int roll(){
    int die1=1+rand()%6;
    int die2=1+rand()%6;
    int sum=die1+die2;
    cout <<"Player rolled "<<die1<<"+"<<die2<<"="<<sum<<endl;
    return sum;
}
int main()
{
    enum Status{CONTINUE,WIN,LOST};
    int myPoint;
    Status gameStatus;  //contain 3 status.

    srand(time(0)); //使用时间变化来设置种子数
    int sumofDice = roll();

    switch(sumofDice){
    case 7:
    case 11:
        gameStatus=WIN;
        break;
    case 2:
    case 3:
    case 12:
        gameStatus=LOST;
        break;
    default:
        gameStatus=CONTINUE;
        myPoint=sumofDice;
        cout<<"Point is " << myPoint<<endl;
        break;
    }
    while(gameStatus==CONTINUE){
        sumofDice=roll();  //roll dice again
        if(sumofDice==myPoint) gameStatus=WIN;
        else {
            if(sumofDice==7) gameStatus=LOST;
        }
    }
    if(gameStatus==WIN) cout <<"You win!"<<endl;
    else cout <<"You lost!"<<endl;
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值