Probability of '0' in random bits

Dear All

Please share your ideas of following talk.
1) there are n random bits consisting of k '0' (e.g. 11110001000110011000...)
2) the probability of '0' at the position of pth, is k/n.
3) need to the write a code to show the result of probability of '0' at the position of (p-1)th, given pth is '0'...which should be (k-1)/n-1 * k/n..

Following is what I did;
1
2
3
4
5
6
7
8
9
10
int main(){
   char buffer[30];
   int n=0;
  for(int i=0;i<sizeof(buffer);i++){
       if(buffer[i]=='0'){
           n++
       }
  }
   cout << n/sizeof(buffer) << endl;
}


Above is for 2nd,
how about conditional probability of 3rd one?
Anyone please shed some light?
Thanks in advance for your attention.
have your ever looked up in the standard library about the functions:
1
2
srand() //random seeding function
rand() // the actual random which generates between 0-1(float) based on seed 

http://www.cplusplus.com/reference/clibrary/cstdlib/

After reading your stuff a little closer, you already have the conditional to see if its zero, ones would be the else. The counter on the for loop is the position in the string for (p-1)th that would make the other equation pretty easy to figure out all in the same loop. The other situation are you assuming that the buffer will be completely full with 1 and 0? I don't see any input for buffer so I can't make any such assumption, k in your thought is size of the buffer when it should be the total count of 1 and 0 if I am not mistaken.
Last edited on
Before we begin, the p=position stuff seems to be confusing you, so forget it for the moment. A lot of computer classes assume you paid more attention in math classes than you actually did.


Normally, in a fair two-outcome probability (in this case, a 0 or a 1), the probability that you get the first outcome (a zero) is 1/2.

Probabilities multiply, so the probability that you get two 0s in a row is (1/2)×(1/2) which is 1/4.

This is because you have an unlimited number of 0s and 1s to draw from. This is called a "fair coin" probability.

______________________________


Your problem changes the probability of drawing a 0 from a 'fair' to a 'biased' probability. This is because you have a fixed number of outcomes (n), and you know that k outcomes are 0s. In other words, you have only a limited number of 0s to draw before you run out. (It is also important that you have a limited number of total outcomes, since k/ is not calculable.)

What this means is that the probability of drawing a 0 as the first outcome will be (number of 0s)/(total number of outcomes), or k/n.

______________________________

The trick now is that once you get an outcome, you cannot get it again. Hence, your number of outcomes is now (n-1). We are also looking at the probability of getting another 0. In order to get this far we have to say that we know we already drew a 0. So the number of 0s is also (k-1).

Again we multiply the probabilities together: (probability of drawing a 0)×(probability of drawing another 0), which is (k/n)×(k-1/n-1).

So, if we have drawn two 0s, what is the probability that we draw yet another 0? The same logic applies.


There are two caveats.
1) What is the probability of drawing (k+1) 0s? (Remember, there are only k 0s to draw.)
2) What is the probability of drawing a 0 if k=n?

______________________________

About your code. Make sure to have an int variable for both k and n -- don't try to use sizeof() tricks.

Also, you don't actually need a buffer full of '0's and '1's. This is a math question. So unless your professor actually asked you for some zeros and ones to look at, don't bother with it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
  int k, n;

  cout << "number of outcomes (n) ? ";
  cin >> n;

  cout << "number of zeros in n (k) ? ";
  cin >> k;

  ...

  return 0;
}

Hope this helps.
Last edited on
Topic archived. No new replies allowed.