注册 登录
编程论坛 C++教室

怎样实现1-33之间的随机数,在线等

liujianlin 发布于 2007-12-16 20:32, 1532 次点击
怎样实现1-33之间的随机数,在线等
4 回复
#2
无缘今生2007-12-16 20:53
// This program seeds the random-number generator
// with the time, then displays 10 random integers.
//

#include <iostream>
//#include <cstdlib>
//#include <cstdio>
#include <ctime>

using namespace std;

int Random(int MIN, int MAX);   // generate a random number

int main()
{
    // set the random-number seed with the current time
    srand((unsigned)time(NULL));

    cout << "请输入要产生的随机数的个数:";
    int NUM;
    cin >> NUM;

    cout << "现在开始产生随机数..." << endl;
    int min, max;

    cout << "请输入要产生的随机数的下限:";
    cin >> min;
    cout << "请输入要产生的随机数的上限:";
    cin >> max;

    //generate the random numbers
    int randoms;

    for (int i=0; i<NUM; i++)
    {
        randoms = Random(min,max);
        cout << randoms << "  ";
    }
    cout << endl;
    system("pause");
    return 0;
}

int Random(int MIN, int MAX)
{
    return (((double)rand() / (double)RAND_MAX)*MAX + MIN);
}
它可以在运行的时间指定随机数的范围.
当然你也可以自己改.
#3
yejingx2007-12-16 21:18
产生一个随机数,模上32,再加1,,
不知道行不?。。
#4
liujianlin2007-12-16 22:39
thank you!!!
1