cplusplus.com
C++ : Forum : Beginners : random using srand() and rand()
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs


solved random using srand() and rand()

keewong (26)
Hi I'm trying to create a program for a school assignment, that requires that it generates 3 random numbers between 3-10.. but the problem I am facing is that it requires that I use 'call by reference', and when using the srand with time as the seed, the computer is so fast, so it essentially generates 3 exactly the same number due to the time being exactly the same.. does anyone have any ideas, how I can get by this? here is my code I have currently. any help greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// code not complete, but just has the essentials that tells you how I call the function,
// and how the function creates the random number..
int main()
{
     InitializeStack(stackA);
     InitializeStack(stackB);
     InitializeStack(stackC);
}

// random number generator function
void InitializeStack(int &stack)
{
     srand(time(0));
     stack =  (rand() % 8) + 3;
}
Last edited on
Bazzy (6258)
Move srand in main
keewong (26)
BEAUTIFUL! Thanks so much Bazzy!
Topic archived. No new replies allowed.