怎样实现1-33之间的随机数,在线等
怎样实现1-33之间的随机数,在线等
// 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);
}
它可以在运行的时间指定随机数的范围.
当然你也可以自己改.