basic probability question

bag has 16 balls
4 are red-12 white
if 2 balls are pulled -what is the prob. 1 is red?
not returning 1st ball or pulling both at same time
how can i calculate this?
1st ball 4/16 ; if white (12/16 it's white)
75% of time 1st is white and still have 4/15 chance to pull red
could someone please show me the formula for this
chances both red?
the bookie at the pub says it's 50%, I know it's lower
Thanks, IGGY
Last edited on
I got 9/20:
You have a 1/4 probability of getting red the first time.
And a 1/5 probability of getting red the second time. ((3/4)*(4/15))
Add them: 9/20
4 are red-10 white
1st ball 4/16 ; if white (12/16 it's white)

Whut?
The chance of both being red is about 1/20th.

You multiply Script, not add.

For kicks:
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
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>

enum Color { Red, NotRed } ;

typedef std::vector<Color> bag ;

bag createBag( unsigned redBalls, unsigned otherBalls )
{
    bag ballBag ;
    for (unsigned i=0; i<redBalls; ++i)
        ballBag.emplace_back(Red) ;

    for (unsigned i=0; i<otherBalls; ++i)
        ballBag.emplace_back(NotRed) ;

    return ballBag ;
}

void randomize(bag & ballBag)
{
    static std::mt19937 engine((std::random_device()())) ;
    std::shuffle(ballBag.begin(), ballBag.end(), engine) ;
}


int main()
{
    const unsigned totalSamples = 1000000 ;
    const unsigned redBalls = 4 ;
    const unsigned otherBalls = 12 ;

    unsigned firstIsRed = 0 ;
    unsigned bothAreRed = 0 ;
    unsigned secondIsRed = 0 ;

    auto ballBag = createBag(redBalls, otherBalls) ;

    for ( unsigned i=0; i<totalSamples; ++i )
    {
        randomize(ballBag) ;

        if ( ballBag[0] == Red )
            ++firstIsRed ;

        if ( ballBag[1] == Red )
            ++secondIsRed ;

        if ( ballBag[0] == Red && ballBag[1] == Red )
            ++bothAreRed ;
    }

    float probFirstIsRed = 
        static_cast<float>(firstIsRed) / totalSamples ;
    float probBothAreRed = 
        static_cast<float>(bothAreRed) / totalSamples ;
    float probSecondIsRed = 
        static_cast<float>(secondIsRed) / totalSamples ;

    std::cout << "Probability first ball is red: " 
              << probFirstIsRed * 100.0 << "%\n" ;
    std::cout << "Probability second ball is red: " 
              << probSecondIsRed * 100.0 << "%\n" ;
    std::cout << "Probability both balls are red: " 
              << probBothAreRed * 100.0 << "%\n" ;
}
Probability first ball is red: 24.9147%
Probability second ball is red: 24.9983%
Probability both balls are red: 5.0139%

35 0 c:\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.6.1\include\c++\random In file included from c:\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/random
3 C:\Dev-Cpp\Programs\000000.cpp from C:\Dev-Cpp\Programs\000000.cpp
32 2 c:\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.6.1\include\c++\bits\c++0x_warning.h [Error] #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
C:\Dev-Cpp\Programs\000000.cpp In function 'bag createBag(unsigned int, unsigned int)':
14 17 C:\Dev-Cpp\Programs\000000.cpp [Error] 'bag' has no member named 'emplace_back'
17 17 C:\Dev-Cpp\Programs\000000.cpp [Error] 'bag' has no member named 'emplace_back'
C:\Dev-Cpp\Programs\000000.cpp In function 'void randomize(bag&)':
24 12 C:\Dev-Cpp\Programs\000000.cpp [Error] 'mt19937' in namespace 'std' does not name a type
25 5 C:\Dev-Cpp\Programs\000000.cpp [Error] 'shuffle' is not a member of 'std'
25 50 C:\Dev-Cpp\Programs\000000.cpp [Error] 'engine' was not declared in this scope
C:\Dev-Cpp\Programs\000000.cpp In function 'int main()':
39 10 C:\Dev-Cpp\Programs\000000.cpp [Error] 'ballBag' does not name a type
43 19 C:\Dev-Cpp\Programs\000000.cpp [Error] 'ballBag' was not declared in this scope


Compiler Errors for Code Above
This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.


Enable C++11 support. And maybe update to a newer version of your compiler.
cire wrote:
Enable C++11 support. And maybe update to a newer version of your compiler.

I am using TDM-GCC 4.6.1, that's pretty new. I will see about C++11 support. Any IDE's that have it?
Last edited on
You enable it by using the compiler flags "-std=c++0x" or "-std=gnu++0x" when invoking the compiler (as indicated in the error message.) Generally, IDEs will have a menu for selecting/setting compiler flags per project. If the one you're looking for isn't present, they will also usually have an option where you can add the flag manually to the command line the IDE uses to invoke the compiler.
On the latest version of g++ you use -std=c++11, not 0x.
sorry- 4 red 12 white
cire: There's a bug in your sampling method. The probability of each of two items independently meeting a condition is not necessarily equal to the probability of either of the same two items meeting it.
For example, in the set {10,11,01}, bit 0 is 1 with P=2/3, and bit 1 is 1 with P=2/3, but (bit 0)|(bit 1) is 1 with P=1.

The value Script Coder arrived at is correct, but the reasoning he used is incorrect.
Probability of first ball not being red: 12/16
Probability of second ball not being red: 11/15
Probability of neither ball being red: (12/16)*(11/15)
Probability of either ball being red: 1-(12/16)*(11/15) = 9/20

Here's the fixed program:
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 <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>

enum Color { Red, NotRed } ;

typedef std::vector<Color> bag ;

void createBag(bag &ballBag, unsigned redBalls, unsigned otherBalls ){
    for (unsigned i=0; i<redBalls; ++i)
        ballBag.push_back(Red) ;

    for (unsigned i=0; i<otherBalls; ++i)
        ballBag.push_back(NotRed) ;
}

void randomize(bag & ballBag){
    std::random_shuffle(ballBag.begin(), ballBag.end()) ;
}


int main(){
    srand(time(0));
    const unsigned totalSamples = 1000000 ;
    const unsigned redBalls = 4 ;
    const unsigned otherBalls = 12 ;

    unsigned firstIsRed = 0 ;
    unsigned bothAreRed = 0 ;
    unsigned secondIsRed = 0 ;
    unsigned eitherIsRed = 0 ;

    bag ballBag;
    createBag(ballBag, redBalls, otherBalls) ;

    for ( unsigned i=0; i<totalSamples; ++i )
    {
        randomize(ballBag) ;

        if ( ballBag[0] == Red )
            ++firstIsRed ;

        if ( ballBag[1] == Red )
            ++secondIsRed ;

        if ( ballBag[0] == Red || ballBag[1] == Red )
            eitherIsRed++;
        if ( ballBag[0] == Red && ballBag[1] == Red )
            ++bothAreRed ;
    }

    float probFirstIsRed = 
        static_cast<float>(firstIsRed) / totalSamples ;
    float probBothAreRed = 
        static_cast<float>(bothAreRed) / totalSamples ;
    float probSecondIsRed = 
        static_cast<float>(secondIsRed) / totalSamples ;
    float probEitherIsRed = 
        static_cast<float>(eitherIsRed) / totalSamples ;

    std::cout << "Probability first ball is red: " 
              << probFirstIsRed * 100.0 << "%\n" ;
    std::cout << "Probability second ball is red: " 
              << probSecondIsRed * 100.0 << "%\n" ;
    std::cout << "Probability both balls are red: " 
              << probBothAreRed * 100.0 << "%\n" ;
    std::cout << "Probability of either ball being red: " 
              << probEitherIsRed * 100.0 << "%\n" ;
}
Last edited on
if 2 balls are pulled -what is the prob. 1 is red?
not returning 1st ball or pulling both at same time


Yes. Somehow I read that as "what is the probability the first ball is red," and given the inarticulate expression of the question, I guess it didn't bother me that it didn't make much sense, and advanced to the question that did make sense to me:

could someone please show me the formula for this
chances both red?


which wasn't the one answered.
@helios my reasoning is perfectly correct, draw a probability tree of the situation, and let me know
I see what tripped me up:
And a 1/5 probability of getting red the second time. ((3/4)*(4/15))

The description doesn't match the actual probability. 1/5 is a probability of getting red the second time (R2) after getting not red the first time (¬R1). This event is disjoint with R1, so
P(R1) + P(¬R1 && R2) = P(R1 || ¬R1 && R2) = P(R1 || R2)
You see, but I should have stated first time and second time, sorry.
Topic archived. No new replies allowed.