Thread: Random numbers works in one program, not in another

  1. #1
    Shadow12345
    Guest

    Random numbers works in one program, not in another

    Hello there, I am trying to use random numbers to kind of simulate evolution...with that said I cannot get the random number generator to work when I want.

    In one practice program I have this:
    Code:
    #include <iostream>
    #include <windows.h>
    #include <conio.h>
    #include <string>
    //#include <time.h>
    
    using namespace std;
    
    int main(void) {
    	
    	int num1; 
    	srand(GetTickCount());
    	num1=rand()%5;
    		cout << "Outputting a random number " << num1 << endl;
    
    	return 0;
    }
    That code works fine for me. However with my human class I have a constructor that tries to initialize all of the attributes to random numbers within 5. It was supposed to be simple but for some reason the numbers are always the same, they nver change. I have tried seeding the random numbers with srand like I did in the previous example but I just can't get random numbers.
    Code:
    //This is the human program
    //The class is class human
    //Here is a list of different attritutes to be included:
    /*
    physical attributes
    [pa]
    	Hair color, eye color, height, weight, skin color, 
    */
    #include <windows.h>
    #include <iostream>
    #include <string>
    #include <conio.h>
    #include <time.h>
    
    using namespace std;
    
    class Human {
    public:
    
    Human(string name);
    
    void OutPutData();
    
    private:
    		string Name;
    		int SkinColor,
    		HairColor,
    		EyeColor, 
    		Height, 
    		Weight,
    		Intelligence;
    
    };
    
    Human::Human(string name) {
    
    	Name = name;
    	srand(GetTickCount());
    	SkinColor = rand()%5;
    
    	srand(GetTickCount());
    	HairColor = rand()%5;
    
    	srand(GetTickCount());
    	EyeColor = rand()%5;
    
    	srand(GetTickCount());
    	Height = rand()%5;
    
    	srand(GetTickCount());
    	Weight = rand()%5;
    
    	srand(GetTickCount());
    	Intelligence = rand()%5;
    };
    
    void Human::OutPutData()	{
    	cout << "Here are the attributes for " << Name << endl;
    	cout << "SkinColor " << SkinColor << endl;
    	cout << "HairColor " << HairColor << endl;
    	cout << "EyeColor " << EyeColor << endl;
    	cout << "Weight " << Weight << endl;
    	cout << "Height " << Height << endl;
    	cout << "Intelligence " << Intelligence << endl;
    	};
    		
    int main(void) {
    	
    	Human Charles("Charles");
    
    	Human Jessica("Jessica");
    
    	Human Bob("Bob");
    
    	Human Frank("Frank");
    
    	Human Emmy("Emmy");
    
    	Human Rachel("Rachel");
    
    	Charles.OutPutData();
    	getch();
    	system("cls");
    
    
    	Jessica.OutPutData();
    	getch();
    	system("cls");
    
    
    	Bob.OutPutData();
    	getch();
    	system("cls");
    
    	Frank.OutPutData();
    	getch();
    	system("cls");
    
    
    	Emmy.OutPutData();
    	getch();
    	system("cls");
    
    
    	Rachel.OutPutData();
    	getch();
    	system("cls");
    
    	
    	return 0;
    }
    If you tell me to do a seach on this subject I'm going to be ........ed off, because I already search and I am trying to do what someone else did, but I don't know where I made my mistake.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    You only need to call srand(GetTickCount()) once in the project....do it at the start of main....

  3. #3
    Shadow12345
    Guest
    Oh cool that seems to have worked. Why is it that you only need to call it once. I thought you had to call it every time before you initialized a variable to rand%somenum;

    That was short and sweet though.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Or a cooler option (IMHO)..

    Give the class a static Randomise function....this can be called without making an instance....

    Code:
    class Human {
    public:
    static void Randomize(void){srand(GetTickCount());}
    Human(string name);
    
    void OutPutData();
    
    private:
    string Name;
    int SkinColor,
    HairColor,
    EyeColor,
    Height,
    Weight,
    Intelligence;
    
    };
    and in main

    Code:
    int main(void) {
    
    Human::Randomize();
    
    //....other code
    
    return 0;
    }
    That will give the desired effect

  5. #5
    Shadow12345
    Guest
    Is there some way you can do random numbers between some positive number and another positive number (or negative for that matter)

    for example x = rand() %10 to 20

    Or is that just being silly?

  6. #6
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    The reason srand() is called only once is because it is the seeding function. After calling this, all subsequent calls to rand() are based upon the seed used ins srand(). Thats why its common to use a time based value.
    Couldn't think of anything interesting, cool or funny - sorry.

  7. #7
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    to get random numbers between 11 and 20:

    srand(GetTickCount());

    cout << "Random Number : " << (rand( ) % 10) +10;
    Last edited by endo; 08-21-2002 at 07:16 AM.
    Couldn't think of anything interesting, cool or funny - sorry.

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    The reason why you dont call it numerous times is that as you were creating those 6 instances so quickly, GetTickCount() was returning the same result each time....so the seed was being reset to the same value over and over again...therefore each subsiquent call to rand was retunring the same result

    >>for example x = rand() %10 to 20

    10 Numbers between 10 & 20

    Code:
    for(int i = 0;i < 10;i++)
    	 cout << (rand() % 11)+ 10 << endl;
    rand()%11 gives any numner between 0 & 10.....then add 10 to get 10 - 20

  9. #9
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    using mod (%) with rand is poor form.
    hello, internet!

  10. #10
    Shadow12345
    Guest
    How is it poor form, is it something you just heard or have you actually found out personally why it is poor form. Poor or not it works.

    Also fordy, how come you use static void, wouldn't void alone suffice?

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Shadow12345
    Also fordy, how come you use static void, wouldn't void alone suffice?
    Its a good place to put stuff that will affect all of the instances of that class as a whole but not individual instances....Also a static func can be called without creating an instance.....

    If you put that code in a constructor as you did originally, it would be called every time an instance was made...leading to the problems you were facing.....

    You could add it as a normal member function and call it on the first instance made.....but I prefer it as above

  12. #12
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Shadow12345
    How is it poor form, is it something you just heard or have you actually found out personally why it is poor form. Poor or not it works.
    nice way to teach people how to code

    do a search it's been discussed before.
    hello, internet!

  13. #13
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Why is it bad form?? I did a quick search and found example after example of rand( ) % someNumber, but I never saw a discussion along those lines. How about filling us in?
    Couldn't think of anything interesting, cool or funny - sorry.

  14. #14
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by endo
    Why is it bad form?? I did a quick search and found example after example of rand( ) % someNumber, but I never saw a discussion along those lines. How about filling us in?
    well obviously you didn't look too hard

    http://www.eskimo.com/~scs/C-faq/q13.16.html
    hello, internet!

  15. #15
    Shadow12345
    Guest
    I already looked at that, and it doesn't make sense:


    "...is poor, because the low-order bits of many random number generators are distressingly non-random"

    Which conflicts with the fact that the method I used works fine...I modified the program to output the numbers generated to a text file so I could look all of them over, and they are distinctly random...I can email it to you if you want...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  2. Need a little help with Cyclic Numbers Program
    By SlyMaelstrom in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2005, 05:01 PM
  3. Generating Random Numbers
    By FromHolland in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2003, 09:05 AM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. Help generating random numbers in MFC
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:52 AM