38、产品随机数的方法

#include<iostream>
using namespace std;
# define n 100
int a[101];

void  p(){
  srand((unsigned)time(NULL)); //获取随机数种子   rand()产品 0-32767 之间的随机数 
  for(int i=0;i<=n;i++){
    a[i]=rand()%100;
  } 
  for(int i=0;i<=n;i++){
    cout<<a[i]<<" ";        
  }
  cout<<endl;
}
/*
产生 0-1 之间的随机数  
*/ 
void  p2(){  
  srand((unsigned)time(NULL)); //获取随机数种子   rand()产品 0-32767 之间的随机数 
  for(int i=0;i<=n;i++){
    cout<<rand()%2<<" ";
  } 
  cout<<endl;
}
//产生在制定范围内的随机数 
void  p3(){
  srand((unsigned)time(NULL)); //获取随机数种子   rand()产品 0-32767 之间的随机数 
  int  low  ,hight;
  cin>>low>>hight;
  for(int i=0;i<=n;i++){
    cout<<(rand()%(hight-low+1))+low<<" ";
  } 
  cout<<endl;
}


//产生制定长度的字符串 
void  p4(){
  srand((unsigned)time(NULL)); //获取随机数种子   rand()产品 0-32767 之间的随机数 
  int  a  ,b;  //表示的是 a 行  b 列。 
  cin>>a>>b;
  string str;
  for(int i=1;i<=a;i++){
     str="";
     
  for(int j=1;j<=b;j++){
     int  temp=rand()%2;
     if(temp==0){
        str+=rand()%(26)+65;            
     }else{
        str+=rand()%(26)+96;    
     }
  } 
   cout<<str<<endl;
  } 
}
int main(){   //产生的伪随机数 
    int j;
    int t=time(0)%10;
    cout<<"   ***趣味摇奖机*** \n\n";
    cout<<"请任选一个数字(0-9):";
    cin>>j;
    if(j<0||j>9){
       return 0;             
    }    
    cout<<t<<endl;
    if(j==t){
      cout<<"特等奖"<<endl;         
    }else if(abs(j-t)<=1){
      cout<<"一等奖"<<endl;      
    }else if(abs(j-t)<=2){
      cout<<"二等奖"<<endl;     
    }else if(abs(j-t)<=3){
      cout<<"三等奖"<<endl;
    }else{
      system("没有中奖哦");      
    }
    p4();
    system("pause"); 
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值