- #1
Lord Anoobis
- 131
- 22
- Homework Statement
- Write a function named coinToss that simulates the tossing of a coin and generates the number of tosses as input by the user.
- Relevant Equations
- None.
The program works as intended.
What I would like to know is why when the randomization is placed within the function (which is what I had done first) does it spit out either all 1s or all 2s?
C++:
#include <iostream>
#include <ctime>
#include <cstdlib>
int coinToss ();int main ()
{
int tosses, result;
std::cout << "How many coin tosses? ";
std::cin >> tosses;
size_t seeder = time(0);
srand(seeder);
for (int i = 0; i < tosses; i++)
{
result = coinToss ();
std::cout << result << "\n";
}
return 0;
}
int coinToss ()
{
int toss;
toss = rand() % 2 + 1;
return toss;
}
What I would like to know is why when the randomization is placed within the function (which is what I had done first) does it spit out either all 1s or all 2s?
C++:
int coinToss ()
{
int toss;
size_t seeder = time(0);
srand(seeder);
toss = rand() % 2 + 1;
return toss;
}