随即问题

Kotoya 2003-08-07 07:45:41
用randomize()的时候要加什么头文件吗? 书上没写啊 不是直接就可以用吗?
还有为什么 RAND_MAX 是对的 LRAND_MAX 就有问题啊 还有random(n)不是和
rand()%n 一样吗? 要怎么改
:\作业\9\1212.cpp(29) : error C2065: '_LRAND_MAX' : undeclared identifier
D:\作业\9\1212.cpp(39) : error C2065: 'randomize' : undeclared identifier
D:\作业\9\1212.cpp(7) : error C2065: 'random' : undeclared identifier
#include <iomanip>
#include <iostream>
#include <cstdlib>
using namespace std;

inline int RandI(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;
}
...全文
85 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZhangYv 2003-08-11
  • 打赏
  • 举报
回复
需要dos.h或者stdlib.h或者time.h忘记具体哪个,都试试.
还是rand()比较好用.
minghui000 2003-08-10
  • 打赏
  • 举报
回复
路过。帮您UP一下。谢谢
leafxx 2003-08-10
  • 打赏
  • 举报
回复
random(n)是随机出0~n的数
和rand()%n的功能一样

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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