   why this srand() function is not workin properly??
#include <iostream>#include <cstring>#include <cstdlib>#include <ctime>using namespace std;class Cbeing;class CWarrior;const int YATAY =11;  // constant related with scene(ekran) and SWorldconst int DIKEY =11;enum gamestate { GAME_INIT=0,GAME_START,GAME_RUN,GAME_ABOUTTOEXIT,GAME_EXIT};   // gamestate enumarations are related with game loopenum characterspecies {being=0,warrior=1};  //will be used for creaturecreatorstruct SWorld    //world that will be accesed by all creatures..{public:char ekran[YATAY][DIKEY];SWorld(){for(int cntr1=0;cntr1<YATAY;++cntr1)// clearing ekran{for(int cntr2=0;cntr2<DIKEY;++cntr2){ekran[cntr1][cntr2]='*';}}}void clear();void Show() const;};void SWorld::clear(){for(int cntr1=0;cntr1<YATAY;++cntr1)// clearing ekran{for(int cntr2=0;cntr2<DIKEY;++cntr2){ekran[cntr1][cntr2]=' ';}}}void SWorld::Show() const{cout<<'\n';for(int cntr1=0;cntr1<YATAY;++cntr1)// clearing ekran{for(int cntr2=0;cntr2<DIKEY;++cntr2){cout<<ekran[cntr1][cntr2];}cout<<endl;}}class Cbeing{private:static unsigned int cntrCbeing;protected:char* name;double life;double mana;int x;int y;public:Cbeing* next;    //this will show the next character on creaturecreatorCbeing():life(2),mana(2),x(0),y(0){name=" ";++cntrCbeing;}Cbeing(char* argname,double arglife,double argmana,int xco,int yco):name(argname),life(arglife),mana(argmana),x(xco),y(yco){++cntrCbeing;}virtual void Show()const;virtual void ekranata(SWorld&) const;};unsigned int Cbeing::cntrCbeing=0;void Cbeing::Show() const{cout<<'\n'<<"Class: ordinary being;"<<endl<<"Name:"<<name<<endl<<"Life:"<<life<<endl<<"Mana:"<<mana<<endl;}void Cbeing::ekranata(SWorld& s1)const{s1.ekran[x][y]='B';}class CWarrior:Public Cbeing{protected:double damagepower;double magicdamagepower;double manaregen;    // LÝFE REGENERATÝONdouble liferegen;    // MANA REGENERATÝONdouble experience;double experincratio;public:CWarrior():Cbeing("anywarrior",10,5,0,0),damagepower(4),magicdamagepower(0),manaregen(0.5),liferegen(2),experience(0),experincratio(1.0){}CWarrior(char* argname,double arglife,double argmana,int xco,int yco,double argdmpow,double argmagdmpow,double argmanareg,double arglifereg,double exper,double experincrat):Cbeing(argname,arglife,argmana,xco,yco),damagepower(argdmpow),magicdamagepower(argmagdmpow),manaregen(argmanareg),liferegen(arglifereg),experience(exper),experincratio(experincrat){}void Show()const;void ekranata(SWorld&) const;};void CWarrior::Show() const{cout<<'\n'<<"Class: Warrior;"<<endl<<"Name: "<<name<<endl<<"Life: "<<life<<endl<<"Mana: "<<mana<<endl<<"DamagePower: "<<damagepower<<endl<<"Magicdamagepower: "<<magicdamagepower<<endl<<"Mana regeneration coefficient: "<<manaregen<<endl<<"Life regeneration coefficient: "<<liferegen<<endl<<"Experience: "<<experience<<endl<<"Experience increase coefficient "<<experincratio<<endl;}void CWarrior::ekranata(SWorld& s1)const{s1.ekran[x][y]='W';}//////////////////////////////CREATURE CREAT0R/////////// A LÝNKLÝST:)class creaturecreator{protected:Cbeing* first;public:creaturecreator(): first(NULL){}Cbeing* create(characterspecies);void Showallcreature();};Cbeing* creaturecreator::create(characterspecies species){srand(time(NULL));switch (species){case being:{Cbeing* temp1=first;  // takes the value of current firstfirst=new Cbeing("karakter",rand()%5,rand()%5,rand()%11,rand()%11);  // first is changedfirst->next=temp1;  //  next is now equal to the previous first;}break;case warrior:{Cbeing* temp=first;first=new CWarrior("karakter",rand()%5,rand()%5,rand()%11,rand()%11,rand()%5,rand()%3,rand()%2,rand()%2,0,1);first->next=temp;}break;}return first;}void creaturecreator::Showallcreature(){Cbeing* temp=first;while(temp!=NULL){temp->Show();temp=temp->next;}}int main(){SWorld world;creaturecreator crecreator;gamestate thatgamestate=GAME_RUN;while(thatgamestate!=GAME_EXIT){switch(thatgamestate){case GAME_INIT:{// codes}thatgamestate=GAME_START;break;case GAME_START:// codesthatgamestate=GAME_RUN;break;case GAME_RUN:{for(int cntr1=0;cntr1<5;++cntr1){if((cntr1%2)!=0){Cbeing* temp=crecreator.create(warrior);temp->ekranata(world);}else{Cbeing* temp=crecreator.create(being);temp->ekranata(world);}}crecreator.Showallcreature();world.Show();}//codesthatgamestate=GAME_ABOUTTOEXIT;break;case GAME_ABOUTTOEXIT://codesthatgamestate=GAME_EXIT;break;case GAME_EXIT:// break;default:{cout<<"\nAn internal error occured.thatgamestate is equaled to GAME_INIT"<<endl;thatgamestate=GAME_INIT; // this is to inform programmer..}}}return 0;}  ;creaturecreator creates 5 creautre but all of them are generated with the same values altough they all have seperated rand() functions   [edited by - tonymontana on February 13, 2004 2:51:45 PM][edited by - tonymontana on February 13, 2004 2:52:45 PM][edited by - tonymontana on February 13, 2004 2:53:33 PM]
  It''s too hard to see the code without the [source ] [/source ] tags.   Kami no Itte ga ore ni zettai naru! ;  ok i have corrected it. ;  Please use source tags, not code tags, it makes it much easier to read.Heres my guess:At the start of CreatureCreator::Create, you seed the random with time.  Your code is probably going so fast, that its seeding the same value or near identical values.What you want to do is seed the random at the start of the program and then never again.So remove srand(time(NULL)) from the start of CreatureCreator::Create and move it to main like so:int main(){srand(time(NULL));SWorld world;creaturecreator crecreator;  ;  you should only seed srand() once...see if that fixes anything ;  ok i did as you said and take the srand() to the beginning lines of main but this just corrected x and y coordinates of creatures.now all of their position is same but their life mana,regeneration point.... are all same still why does anyone(or you) have any idea? ;  First, you have a huge bug waiting to happen in your Cbeing class. You never allocate memory for the name value, which is okay if you always pass a buffer that has a lifetime equal to or longer than the Cbeing instance. Otherwise, access violation.Second, did you remover the srand() call from the CreatureCreator::create method? You should call srand() only once, in general, per program. (It''s really a little more complicated, but this is a good rule of thumb.)As for their attributes being the same: rand() isn''t that random, and it''s low bits are even less random than its overall value. Since you use the modulus operator (bad idea), you get those low bits - and thus virtually the same values! ;  first of all thanks oluseyi.. but i want to ask some questions newhow can i find a good tutorial about these accss violation ond buffer issues(i really do not know anything about them although i am about to finish a book on c++ (actually i read 400 pages over/700)second why using modules operator is a bad idea?? and is there abetter random number generator(there must be i thought) than srand() which is also generatin float/double numbers..thanks for your response ;quote:Original post by tonymontana  first of all thanks oluseyi...You''re welcome.quote:how can i find a good tutorial about these accss violation ond buffer issues?The Memory Management Reference.quote:second why using modules operator is a bad idea?The Evils of rand().quote:and is there a better random number generator?Mersenne Twister.quote:[One] which is also generatin float/double numbers...Boost Random Number LibraryThe RNG Random Number Generator (warning: heavy math and theory). 