srand and rand for random numbers

I'm trying to make a function that will produce three random numbers every time its called. I've read up on srand to seed the random number generator, but the description seems kind of basic. I understand srand() to grab a set of numbers that rand will randomly choose from.
The first way I tried will produce a random number each time the program is run, but not every time the function is called. And it's 3 of the same number each time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

void cast( void ); 

int main( void ) {

   cast();

   return 0;

}

void cast( void ) {

   int num;

   for ( int i = 0; i < 3; i++ ) {

   srand( time( NULL ) );  //  using the time seed from srand explanation 
   num = rand();
   cout << num << endl;

   }

   return;

}


The second way I tried will give me three different numbers once the function is called, but always the same set of numbers, even when the program is rerun.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

void cast( void ); 

int main( void ) {

   cast();

   return 0;

}

void cast( void ) {

   int num;

   for ( int i = 0; i < 3; i++ ) {

   srand( i + 1 ); // using i to get different srand values
   num = rand();
   cout << num << endl;

   }

   return;

}


It seems like you need a random number generator to seed the random number generator. It doesn't make sense if you srand( 2 ) for example, why all the rand() from the 2nd seed would always been the same number?
In computers, there is no such thing as a "random number" -- only pseudo-random numbers.

You can choose from a whole collection of pseudo-random number sequences. You do this by seeding the generator using srand().

You can pick a specific sequence by passing specific integer to the srand() function, so that every time you run your program it uses the exact same sequence of pseudo-random numbers, yes.

Usually, however, people want the program to behave differently each time it is run. So you are correct that a relatively random seed must be used. This is where the time() comes in. What better choice of randomness than the time the user launched the program?


You only need to seed the (P)RNG once, at the beginning of the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

void cast();

int main() {
   // Seed the RNG once, at the start of the program
   srand( time( NULL ) );

   // Roll our die
   cast();

   // All done
   return 0;
}

void cast() {
   for (int i = 0; i < 3; i++) {
      // Get a pseudo-random number in [1,100]
      int num = (rand() % 100) + 1;
      cout << num << endl;
   }
}

Please don't throw random void and returns in there.

Hope this helps.
Ahhh I see. I was always re-seeding the same set every time the function was called! I kind of get it now. Thanks for the help.


please don't throw random void and returns in there.


I picked this up from my teacher I had for both C and C++.

He'd also flip out if we did this:

1
2
3
4
int foo( arg ) 
{ // Oh no you didn't!  This { has to be right AFTER the function signature !!!! 
   return arg;
}


is there any technical difference between
1
2
3
void foo( void ) {
   return;
}


and

1
2
3
void foo() {

}


or does it fall under programming conventions? Thanks again!!
braces
People who flip out on brace placement are losers.
http://en.wikipedia.org/wiki/Indent_style


voids and returns
The technical difference between the two is that the first is only tolerated by C++ compilers while the second is correct C++.

It is a C99 convention to indicate that there are no arguments by placing a void in there. C++ is based upon C89/90, which eschews that convention. In C++, an empty argument list specifically indicates a function taking NO arguments.

While it may be chalked-up to convention, it is pretty universally considered poor C++ style to have void argument lists and explicitly return at the end of a void function.
(Personally, I think that boilerplate like that is part of the bean-counter syndrome suits like to impose on programmers.)


In actual life, you have to deal with code that often has a lot of different styles applied to it. The important thing is that it is readable. It is not important whether the brace is on the same line or not. Any C or C++ programmer worth his salt can read both with equal ease without even a passing thought.

Hope this helps.
Topic archived. No new replies allowed.