C++从0-100产生随机数

当随机数小于50时,输出小于;反之。当按下enter再次产生一个随机数,并输出小于或大于

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    int n,t=1;
    srand((unsigned int)time(NULL));
    while(t){
      //ctrl+c 结束循环
        n=rand() % 100 ;//生成0-100的随机整数        
        if (n<50){ //小于50的 
           cout<<n<<" 小于"<<endl;           
        }
         if (n>50){ //大于50的,标记 
           cout<<n<<" 大于"<<endl;           
        }
       t=cin.get();//回车继续运行循环
    }  
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-08-09
c++ 程序,按 Ctrl+c 退出,enter 继续。
#include<iostream>
using namespace std;
#include<stdio.h>
#include <time.h>

int main()
{
int v;
srand(time(0));
while(1){
v=rand()%101;
if (v<50) printf("%d < 50\n",v);
else printf("%d >= 50\n",v);
getchar();
}
return 0;

}
第2个回答  2018-08-09
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main( ) 
{    
    srand( (unsigned)time(NULL) );
    
    char op;
    int  randNum;
    do{
        cin.get(op);
        if( op == '\n' ){
            randNum = rand()%101;
            if( randNum < 50 ){
                cout << randNum << " 小于\n"; 
            }else{
                cout << randNum << " 大于\n";
            }
        }else{
            break;
        }
    }while( true );
    
    return 0;
}