srand question

This is a discussion on srand question within the C++ Programming forums, part of the General Programming Boards category; Should I call srand just in main(), or in every function that needs randomosity?...

  1. #1
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369

    srand question

    Should I call srand just in main(), or in every function that needs randomosity?
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    It's best to seed the generator only once in the program :-)
    *Cela*

  3. #3
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Thanks!
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  4. #4
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    EDIT: This post contained invalid statements, so I have edited it. Sorry for this screw up of mine.
    Last edited by Munkey01; 02-12-2003 at 08:51 PM.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    It really doesn't matter how many times you seed it. Just don't do it at EVERY rand, its very inefficient. Always remember to do it at least once in your program, maybe the first statement in main().

  6. #6
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    I was wrong. I just wrote a short little test and ended up that I was wrong. Here is my test-

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    int main()
    {
    	srand(time(NULL));
    	for (int i=0; i < 10; i++)
    	{
    		cout << rand() % 10;
    	}
    	return 0;
    }
    Output-
    8
    2
    7
    9
    1
    4
    5
    2
    1
    2
    So my appologies to everyone. I will edit my post so that no one gets a wrong statement stuck in their head.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21