一个判断猫是否会打架的问题!我的随机数产生好像有问题!

ztwz 2004-12-12 06:27:50
//问题:为什么我的随机数产生的都是同一个数?我以用时间做过种子了!
#ifndef cat_h
#define cat_h
class cat
{
public:
cat();
const char *getFurColor() const;
const char *getHairLength() const;
const char *getEyeColor() const;
private:
void setFurColor();
void setHairLength();
void setEyeColor();
char *furColor;
char *hairLength;
char *eyeColor;
};
#endif
-------------------------------------------------
#include <iostream>
#include <time.h>
#include "cat.h"
using namespace std;

cat::cat()
{
srand(time(0));//我做种了呀 ̄ ̄ ̄ ̄ ̄
setFurColor();
setEyeColor();
setHairLength();
}

const char *cat::getFurColor() const //返回猫的毛皮色
{
return furColor;
}

const char *cat::getEyeColor() const //返回猫的眼睛颜色
{
return eyeColor;
}

const char *cat::getHairLength() const //返回猫的毛长短
{
return hairLength;
}

void cat::setFurColor() //随机产生毛皮颜色
{
int x=rand()%3;
if(x==0)
furColor="black";
else if(x==1)
furColor="gray";
else
furColor="brown";
}

void cat::setEyeColor() //随机产生猫眼睛颜色
{
int x=rand()%3;
if(x==0)
eyeColor="bule";
else if(x==1)
eyeColor="brown";
else
eyeColor="green";
}

void cat::setHairLength() //随机产生毛的长短
{
int x=rand()%2;
if(x==0)
hairLength="short";
else
hairLength="long";
}
-------------------------------------------

#include <iostream>
#include <stdlib.h>
#include "cat.h"
using namespace std;

void check(cat *pen[],int);

int main(int argc, char *argv[])
{
const int pen_size=7;
cat *pen[pen_size];
for(int c=0;c<pen_size;c++)
{
pen[c]=new cat;
}
for(int c=0;c<pen_size;c++)
{
check(pen,pen_size);
cout<<"Press a key to add the next cat."<<endl;
cin.get();
}
for(int i=0;i<pen_size;i++)
delete pen[i];
system("PAUSE");
return 0;
}

void check(cat *catpen[],int numberOfCats)
{
int brownCats=0, grayCats=0, blueEyes=0,
greenEyes=0, brownEyes=0;
for(int x=0;x<numberOfCats;x++)
{
if(strcmp(catpen[x]->getFurColor(),"gray")==0)
++grayCats;
if(strcmp(catpen[x]->getFurColor(),"brown")==0)
++brownCats;
if(strcmp(catpen[x]->getEyeColor(),"blue")==0)
++blueEyes;
if(strcmp(catpen[x]->getEyeColor(),"green")==0)
++greenEyes;
if(strcmp(catpen[x]->getEyeColor(),"brown")==0)
++brownEyes;
}
if(grayCats>brownCats)
cout<<"The gray cats are fighting with the brown cat"
<<(brownCats>1?"s.":".")<<endl;
cout<<"brownCats:"<<brownCats<<"\n"<<"grayCats:"<<grayCats<<endl;
//if()//这儿还没写完,要求是当围栏里至少一只一只黑色绿眼的猫,和至少一只黑色蓝眼的猫时,哪果在放入一只且仅为一只黑色的棕眼猫,黑色的猫之间就会打架,如果围栏里的黑色棕眼猫多于一只,黑猫之间就不会打架
cout<<"The black cat(s) with blue eyes and the black"
<<"cat(s) with green eyes are\nfighting with the"
<<" black cat that has brown eyes."<<endl;
}
...全文
229 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
beyondtkl 2004-12-13
  • 打赏
  • 举报
回复
:-)
xuzheng318 2004-12-13
  • 打赏
  • 举报
回复
赞同楼上的,帮顶 接分!
pacman2000 2004-12-13
  • 打赏
  • 举报
回复
程序只要刚开始调用一次srand就可以了,否则,srand种子一样的话,出来的rand系列是相同的!!!
storm2003 2004-12-13
  • 打赏
  • 举报
回复
赞同mathe()
mathe 2004-12-13
  • 打赏
  • 举报
回复
计算机速度太快了:)所以每次执行srand(time())时,由于time()返回的数值都相同,
也就是对于不同的cat,都给了相同的伪随机种子。这里说伪随机是因为rand()不是真正的
随机数发生器,而是对于相同的种子,后面产生的序列都是相同的。
比如你可以试一下
srand(1), rand(), rand(),rand()
不管你调用多少次,三次rand()产生的数字都会相同。
同样,可以试验一下用其他种子,比如
srand(2),rand(),rand(),rand()
这时,产生的序列就会和刚才的不同。
在一般程序中,我们需要保证srand()只被调用了一次。
hchinside 2004-12-13
  • 打赏
  • 举报
回复
调用一次就好了,不要在构造里调用
greenteanet 2004-12-13
  • 打赏
  • 举报
回复
:)
qufan 2004-12-13
  • 打赏
  • 举报
回复
nod
同意楼上的
quieter 2004-12-13
  • 打赏
  • 举报
回复
想不通,关注一下!up
ztwz 2004-12-12
  • 打赏
  • 举报
回复
但是为什么入到构造函数里就不行???
ztwz 2004-12-12
  • 打赏
  • 举报
回复
结帖,把时间种子放到main()里就行了!
失败!
Vinc 2004-12-12
  • 打赏
  • 举报
回复
别用srand试试,以前我也有过类似的问题。
truewill 2004-12-12
  • 打赏
  • 举报
回复
呵呵,好像时间差不多的时候,第一个随机数也是差不多的
不过好像舍弃掉开始的2 3 个就好了
chunhai12 2004-12-12
  • 打赏
  • 举报
回复
//a random number generator class
#include <iostream>
#include <ctime>

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);
}

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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