coin toss simulation

Hi everybody:

I did this program simulating a coin toss

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//coin toss simulation
#include <iostream>
#include <cstdlib>
using std::cout;

int main()
{
    int num, streak,max_streak;
    longint m
    streak=0; max_streak=0;
    for (m=1; m<=100000000; m++)
    {   num=rand()%2;
        if (num==0) streak=streak +1;
        else {
            if(streak>max_streak) max_streak=streak;
            streak=0;
        }
       // cout<<setw(8)<<num;
     }
    cout<<"max streak "<<max_streak;

    return 0;
}



The max streak for tails/heads I get is 15 , Am I right?
Last edited on
Are you trying to count how many times you throw a head/tail one after each other? That is what max streak is.
Last edited on
The max streak for tails/heads I get is 15 , Am I right?

That is totally going to depend on the sequence of random numbers returned by rand() which is implementation specific.

You're not seeding the random number generator by calling srand(). That means you're going to get the same sequence of pseudo-random numbers every time you run your program. You should call srand() once at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/

#include <ctime>

srand(static_cast<unsigned int>(time(NULL))); // randomise seed
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
//coin toss simulation
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;

int main()
{
    int num, streak,max_streak;
    
    srand(static_cast<unsigned int>(time(NULL))); // randomise seed
    
    streak=0; max_streak=0;
    for (int m=1; m<=100000000; m++)
    {   num=rand()%2;
        if (num==0) streak=streak +1;
        else {
            if(streak>max_streak) max_streak=streak;
            streak=0;
        }
       // cout<<setw(8)<<num;
     }
    cout<<"max streak "<<max_streak;

    return 0;
}

james it gives max_streak ==15 each of the four times I ran the program
Gunnerfunner I only added the srand function and changed the for statement to int m = 1;
Otherwise the code is as was. I assumed because he kept getting the same result it was due to not randomising?

I hope I haven't given a bum steer

I've ran it 5 times and get 24, 26, 25, 25, 23
Last edited on
interesting, now at cpp.sh it gives 25 four consecutive times. Are you running exactly the same code?
sorry gents, may be I didnt explain myself well.

I expect the streak to increase (maybe a little) every time I increase the for cycle. However I still get the 15 limit.

Im still gettin the 15 limit using jamesfarrow option.

I'm using codeblocks+GNU GCC compiler
gunnerfunner - yeah I've just ran my rendition and got varying results from 29 to 23

It tossing a coin and counting consecutive nunbers thrown? Or have I misread it?

Surely there is a point/number where no matter how many times you run the loop you will always get around the same result because its random - if you get my meaning
Last edited on
It tossing a coin and counting consecutive nunbers thrown? Or have I misread it?


Yep, you are right.

when I use C++ shell I got what both of you say, but when using gnu cc I still get the same.
I added code to print out m whenever it gets a new max streak size:
1
2
3
4
            if (streak > max_streak) {
                max_streak = streak;
                cout << "streak length " << max_streak << " ended at m = " << m << '\n';
            }

Running it on my PC I get:
streak length 1 ended at m = 4
streak length 5 ended at m = 10
streak length 6 ended at m = 433
streak length 10 ended at m = 511
streak length 12 ended at m = 5244
streak length 13 ended at m = 18151
streak length 16 ended at m = 99349
streak length 20 ended at m = 225161
streak length 22 ended at m = 6404348
streak length 25 ended at m = 21024497
streak length 28 ended at m = 92173082
max streak 28

As you can see, each new record takes nearly an order of magnitude more tosses to set each new record.
May be I shoould change compiler

Thanks a lot jamesfarrow, AbstractionAnon, gunnerfunner and dhayden! :)
Topic archived. No new replies allowed.