C++ 控制台实现产生抽奖随机数

闲来无事,学到了Rand函数的用法,于是想到实现一个控制台抽奖的程序

程序功能比较简单,输入一个数值作为随机数的范围,为[1,range],通过不断按回车产生随机数,停止回车时固定。通过system(“cls”)实现清除之前的数据。输入任意其他字符+回车可以更换随机数范围。Ctrl+Z可停止程序运行。


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

int main()
{
    int range;
    while(cout << "输入随机数的范围 : ", cin >> range)
    {
        int ch;
        srand(time(NULL));
        while(ch = cin.get())
        {
            system("cls");
            if(ch == '\n')
                cout << "随机数 : " << (int)((rand()*1.0/RAND_MAX)*range) + 1 << endl;
            else if(ch == -1)
                return 0;
            else
                break;
        }
    }
    return 0;
}





posted @ 2014-12-04 16:36  Popco  阅读(1187)  评论(0编辑  收藏  举报