Related to random numbers

// this program gives random number output
#include <iostream>
#include <cstdlib> // contains function protype for rand
#include <iomanip> // for setw
using namespace std;
int main()
{
unsigned int seed;
cout << "enter seed\n";
cin >> seed;
srand (seed);
// loop 20 times
for (int counter =1; counter <= 20; ++ counter)
{
//pick random no ffrom from 1 to 6 and output it
cout << setw(10) <<(1+rand ()%100);//100 is called scaling facter & output is b/w 1 to 100
if (counter % 5==0) //if counter is divisible by 5, start a new line of output
cout << endl;
}
return 0;
}

/*my question is what is the effect on output of program of different numbers input to the int data type named seed*/
rand() doesn't actually give you a random number. It gives you a pseudo random number.

Computers generate pseudo random numbers with what is essentially a mathematical equation. The idea is it has a state that gets "randomized" based on running its previous state through the equation.

"seeding" the random number generator merely gives it its initial state... ie: it gives the equaltion a starting point.

So rand and srand might look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
int random_state;

void srand(int seed)
{
    random_state = seed;
}

int rand()
{
    random_state = (random_state * 498721) + 7431;
    return random_state;
}


(note of course... this example is merely conceptual and is greatly simplified... rand and srand will likely operate very differently and likely will be more complex).
Topic archived. No new replies allowed.