获得随机数,需包含哪个头文件?

zcsd 2003-08-30 11:07:45
#include<iomanip>
#include<iostream>
#include<cstdlib>
//-----------------------------
using namespace std;
//-----------------------------
inline int RandId(int N)
{
return random(N)+1;
}
//-----------------------------
void TestDice();
const int TestNum=6000;
//-----------------------------
int main()
{
cout<<setiosflags(ios::right)
<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint)
<<std::setprecision(4);
cout<<"RAND_MAX(0x7FFFU)的值是:"
<<setw(7)<<RAND_MAX<<endl;
cout<<"LRAND_MAX(0X7FFFFFFFU)的值是:"
<<setw(7)<<LRAND_MAX<<endl;
TestDice();
return 0;
}
//-----------------------------
void TestDice()
{
int Freq[6],Face,i;
for(i=0;i<6;i++)
Freq[i]=0;
randomize();
cout<<"连掷20次的结果:"<<endl;
for(i=1;i<=20;i++)
{
cout<<setw(5)<<RandI(6);
if(i%5==0)
cout<<endl;
}
cout<<endl;

for(int Roll=0;Roll<TestNum;Roll++)
{
Face=RandI(6);
Freq[Face-1]++;
}
cout<<" 点数 次数"<<endl;
cout<<"----------"<<endl;
for(i=0;i<6;i++)
cout<<setw(5)<<(i+1)
<<setw(10)<<Freq[i]<<endl;
cout<<"----------"<<endl;
return;
}

编译错误为:
'random' : undeclared identifier
'LRAND_MAX' : undeclared identifier
'randomize' : undeclared identifier
'RandI' : undeclared identifier

请高人相助!

加注:本人用的是VC6.0

...全文
730 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
overawe 2003-09-20
  • 打赏
  • 举报
回复
//借花献佛 书上源码

#include <iostream>
#include <time.h>

using namespace std;

// generate random numbers
class randomNumber
{
public:
// initialize the random number generator
randomNumber(long s = 0);

// return a 32-bit random integer m, 1 <= m <= 2^31-2
long random();

// return a 32-bit random integer m, 0 <= m <= n-1,
// where n <= 2^31-1
long random(long n);

// return a real number x, 0 <= x < 1
double frandom();

private:
static const long A;
static const long M;
static const long Q;
static const long R;

long seed;
};

const long randomNumber::A = 48271;
const long randomNumber::M = 2147483647;
const long randomNumber::Q = M / A;
const long randomNumber::R = M % A;

randomNumber::randomNumber(long s)
{
if (s < 0)
s = 0;

if (s == 0)
{
// get time of day in seconds since 12:00 AM,
// January 1, 1970
long t_time = time(NULL);

// mix-up bits by squaring
t_time *= t_time;
// result can overflow. handle cases
// > 0, < 0, = 0
if (t_time > 0)
s = t_time ^ 0x5EECE66DL;
else if (t_time < 0)
s = (t_time & 0x7fffffff) ^ 0x5EECE66DL;
else
s = 0x5EECE66DL;
}

seed = s;
}

long randomNumber::random()
{
long tmpSeed = A * ( seed % Q ) - R * ( seed / Q );

if( tmpSeed >= 0 )
seed = tmpSeed;
else
seed = tmpSeed + M;

return seed;
}

long randomNumber::random(long n)
{
double fraction = double(random())/double(M);

return int(fraction * n);
}

double randomNumber::frandom()
{
return double(random())/double(M);
}
manlian 2003-09-20
  • 打赏
  • 举报
回复
大家共享怎么样
kotton8848 2003-09-01
  • 打赏
  • 举报
回复
只有编译器有了一个使用srand函数定义的randomize()的例程,才能调用这个函数
你可以用srand(time(null))来改变
用的预处理头文件是#include<time.h>
文什么我用的书的头文件是<stdlib.h>阿?

atEleven 2003-08-31
  • 打赏
  • 举报
回复
估计是各个编译器的兼容问题.
overawe 2003-08-31
  • 打赏
  • 举报
回复
看过TC++PL 吗
好象直接用rand 不太好
zcsd 2003-08-31
  • 打赏
  • 举报
回复
用srand(time(0));和rand();可以搞定。谢谢!

追加一个问题:
randomize();这是个获得随机数种子的函数,为何VC中总是不能辨识,已经#include<cstdlib>了。
同样,也不能辨识LRAND_MAX。但可辨识RAND_MAX,为32767。
望大侠释疑。
atEleven 2003-08-31
  • 打赏
  • 举报
回复
就在CStdLib里面.

不过用的是 srand 和 rand 两个函数.

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧