how to make straight 9 head or tale.?

i am having a coin flip problem.
there's no prob with this code but my prof wanna make this difficult.
anyway what i wanna know is what could be the result of
tale comes 9 times straight ahead and the last one could be tale or head that that doesnt matter. what i wanna know is how many times shold i throw a coin so that i can get tale or head , straight 9 times??? plz help me ;(

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
72
73
74
75
76
77
#include "stdafx.h"
#include <iostream>
using namespace std;

int coin;
int hcount, tcount;
int itr;


void init()
{
	//initialization
	coin = 0;
	hcount = 0;
	tcount = 0;
	itr = 0;
}

void getinputitr()
{
	cout << "How Many Times Do You Want To Try??  ";
	cin >> itr;
}

void TrowCoin()
{
	// code body 
	for (int n = 0; n < itr; n++)
	{
		for (int i = 0; i < 10; i++)
		{
			
			coin = rand() % 2; // 0 is head and 1 is tail 

			if (coin == 0 ) 

			{
				
				//cout << "the result is Head"<< endl;
				hcount++;
			
			}
			

			else
			{
				//cout << "the result is Tail" << endl;
				tcount++;

			}
			

		}
	

		if ( hcount >=9 || tcount >= 9 )
		

		cout << n + 1 << ". toal count of head is " << hcount << 
                       "  tail is " << tcount << endl;
		hcount = 0;
		tcount = 0;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	init();
	getinputitr();
	TrowCoin();

	return 0;
}



 
Last edited on
That boils down to a simple mathematics problem then. What are the odds of one coin flip ever being heads or tails? 50%, or 1/2 as your case may be. Now take that to the power of 9, for the number of heads you'll want. The last one (for the 10th flip) is going to be a 1x multiplier. Why? Because in your case, your professor doesn't care about the last result.
thank you for you reply but what i really wanna knw is
what could be possiblity of hving "hhhhhhhhht" or "ttttttttth"
in that code i upload i can get the possiblity of 9tale or 9 heads but its like
"hthhhhhhhh" or "thhhhhhhhhh" ... get my point?
The probability of getting a heads or tails on the first flip is 0.5. This can be written as P(H) = 0.5, where P is the probability notation and H is the event. Likewise with tails: P(T) = 0.5

The probability of getting a heads, then another heads is 0.25, since we can have 4 possible outcomes on the second flip. That is: (H,H) (H,T) (T,H) (T,T). Since on the second flip the probability of getting a heads or a tails is also 0.5, we can multiply it by the first flip.
Therefore, P(H,H) = 0.5 * 0.5
= 0.25
Likewise, P(H,T) = 0.5 * 0.5
= 0.25
From this, we can see that no matter the combination the probability of that occurring is the same. Also we also notice that the probability can be generalised. The P(H,H) = 0.52; P(H,H,H) = 0.53, etc...
Therefore on the ninth flip, P(H,H,H,H,H,H,H,H,H) = 0.59
= 0.001953125
This is the same for all events on the ninth flip, no matter how many heads or tails you get.

I have a pdf to demonstrate this: https://www.dropbox.com/s/xagjmqr0o4937xf/Coin%20Probability.pdf?dl=0

Also, you have to seed the rand function, otherwise it'll output the same result every time.
http://www.cplusplus.com/reference/cstdlib/srand/
Last edited on
What I wrote for you, @OP, and what @integralfx explained in different detail was to get the probability of getting whatever result you wanted. Especially since a coin flip only involves two choices, any choice other than "I don't care" will result in a multiplier of 0.5 or 50% or 1/2. And again, as mentioned before, take that to whatever power you want (and by power, I mean however many head/tail combinations you want).

For example, you want to know the probability of getting H,H,T,T? Take that 1/2 to the power of 4. etc. etc.
Also, since the probability of any event on the ninth flip is 0.001953125 (1/512), and we want that probability to be 1, we take the reciprocal of that probability, that is, 1/(1/512), which is just 512. -> 1/512 * 512 = 1

Therefore, you'll need to flip the coin 512 times.

Can someone clarify if 512 is the minimum or maximum amount of flips needed? I think it's the maximum, but don't take my word on that.
Last edited on
That's going to be average case probability. Just like flipping a coin once, your chances of getting either heads is 50%. But you might get unlucky and get tails. And get tails again. However, as the counter for an undesired result increases, the chances of a desired result occurring increases.

In other words, on any given day, to get 9 heads or 9 tails straight in a row, you'll need about 512 coin flips.
closed account (48T7M4Gy)
Can someone clarify if 512 is the minimum or maximum amount of flips needed?


It is neither the maximum nor the minimum. It is relevant as the expectance/expected frequency and that's about all we can say.

For instance the real and absolute minimum is 9 throws - get all on the first nine.
There is no maximum, ie there is no guarantee 9 heads will ever occur.

The 1/512 number means if you throw 100,000 (ie tons) of times then we can expec 10000/512 occurences. But again there are no guarantees. Try it on the computer simulation.

There is a rough rule of thumb based on the normal distribution that in 512 throws the likelihood of 9 is about 64%. So in other words the reality is you'll need more than 512 days to get more than a 64% chance. That now depends on whether you're prepared to take a bet on those odds - a personal risk choice.
If I understand you correctly... You can use this...

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
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

int getinputitr(int);
int results(int);


int main()
{

    int number=0;
    int flip=0;

    srand(time(0));

    int tries = getinputitr(number);

    for ( int x=0; x<tries; x++)
    {
        flip = (rand()% 2);
        int answer = results(flip);
    }
    return 0;
}

int getinputitr(int tries)
{
    cout << "How Many Times Do You Want To Try??  ";
    cin >> tries;
    return tries;
}

int results(int flip)
{
    if (flip==0)
    {
        cout << "Heads it is." << endl;
        return flip;
    }
    else
    {
        cout<< "Tails it is." << endl;
        return flip;
    }
}


Change or remove the getinputitr function, and put in an endless loop. When the result function returns, place the result into a vector, or an array... every nine flips add the elements of the array. If it's zero you got it. If it's 9, you got it. Add a counter to count the total number of flips and you will have you the answer you seek.
Last edited on
@kemort
Thanks for the explanation. :)
I've made a program that flips it 100,000 times and averages it. It always returns 582 or 583.
http://pastebin.com/M62NjYhh
Formatting broke :/

It takes about 10 seconds for me to run but that varies depending on your CPU. I have an i7, by the way.
Last edited on
closed account (48T7M4Gy)
Well done integralfx ... it depends too on how random the randomising functions we use are too. But overall it's amazing how long it takes for things to converge to the pure/theoretical value. I guess that's what gaming machines capitalise on.

( i7's go well - it's a even scarier what the big machines are capable of, 100,000 in less than a blink. )
@kemort
I appreciate the praise :3

I created a multithreaded version which maxes out my i7.
Enjoy :)
http://pastebin.com/0XtzJfXQ
Can you make something equivalent to the single-threaded version with a single loop and no arrays? Should take roughly 20 lines.
@helios
Are you asking me to do that? Because, I'm not as experienced in C++ as you are so for you it should be easy.

edit: I had a try at it... but I'm tired so correct any mistakes please. :)
http://pastebin.com/Za4dPqGm

I'm having a slight problem...
http://pastebin.com/uBCaeGnL

Number of trials needed: 1.20641e+07
Press enter to exit...
Last edited on
It's a challenge. Try to find a way to do it.

You also don't need recursion. You can make something equivalent with just
1
2
3
4
5
6
7
8
//etc.
int main(){
    //initialization
    for (/*...*/){
        //???
    }
    //print results
}

Hint: 0 <= rand() <= RAND_MAX, and RAND_MAX is (usually) a power of 2 minus 1.
Last edited on
Topic archived. No new replies allowed.