Stuck on my project

Oct 27, 2015 at 7:44pm
Hi guys im so close to finishing my project of building a coinflip game where you choose the percentage chance of it landing on heads and how many times u want it to repeat the flip. It reads those two by taking the first command line argument (reps) and second command line argument (percent chance). Now I believe i have everything ready to go except that when i run my bool function it runs one unique time and just reprints out the same result 'reps' amount of time instead of running uniquely 'reps' amount of time and print the results out. I know i have to do something with that for loop but i just cant figure out how to get around it. ive been stuck for like an hour with this please give me advice!
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cctype>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>


using namespace std ;

bool gameflip(int pct)
    {

        time_t now;
        time(&now);
        srand(now);
        rand();


        int random = rand() % 100;
           cout << random;      //outputs random number




       if (random<= pct)       //random is randomizer
       {                           //pct is user input for heads chance
           return true;
       }
       else                 //tails
       {
           return false;
       }




    }
int main (int argc, char *argv[])
{
    int rep = atoi(argv[1]);           //receives repetition amount
    int chance = atoi(argv[2]);        //receives chance amount out of 100

    cout<< rep <<" reps"<<endl;
    cout << chance<<"% chance"<<endl;

    if (chance <101 && chance > 0)
    {
    for(int i=0;i<rep;i++)                 //runs how many rep times
    {
       // gameflip(chance);
        if(gameflip(chance)== true)
        {
           cout <<"\t"<<i+1<< "\theads\t"<< chance<<endl;

        }
        else
        {

            cout <<"\t"<<i+1<<"\ttails\t"<< chance<<endl;
        }
    }
    }

    else
    {
        cout <<"Please enter a percentage between 0 and 100.";
    }

Last edited on Oct 27, 2015 at 7:50pm
Oct 27, 2015 at 8:31pm
You should only call srand() once, usually early in main(). In your fast loop the seed will probably never change, hence the duplication.

Last edited on Oct 27, 2015 at 9:09pm
Oct 27, 2015 at 8:49pm
are u suggesting i move

time_t now;
time(&now);
srand(now);
rand();

to main()?
Oct 27, 2015 at 8:51pm
omg thank you now i can move forward. im so dumb i should have realized that.
Oct 27, 2015 at 9:13pm
By the way that call to rand() is not necessary.
Topic archived. No new replies allowed.