Probabilty is off

So I am trying to calculate the probability that triangles will be formed from a set amount of glass rods and I can not get it to come out right. The lower the amount of glass rods the higher the probability is, but its over 100% which obviously isn't right, and then when there are a lot of glass rods the probability is 0, here is my code..
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<iomanip>

using namespace std;
//Protocols
//Determining amount of triangles formed from rods
int get_tri(int&);
//Gets intial information for program
void get_info();
//Determines probability of triangles formed from amount
//of rods broken
float tri_prob(int, int, float&);

int main()
{
    srand(time(0));     //sets srand to pick random numbers
   
    //Title and purpose of program
    cout << "Broken Glass Simulator:"<< endl<<endl;
    cout << "This simulator determines how many triangles"<< endl;
    cout << "are formed from a set amount";
    cout << " of dropped glass rods"<< endl<< endl;
    
    get_info();
    
}

void get_info()
{
    const int SENTINEL = 21;    // Sentinel value to exit program
    int num_rods;       //Number of glass rods dropped
    int num_tri;        //Number of triangles formed
    int triangles=0;  //amount of triangles formed
    float prob;         //probability of triangles formed
    int amt;
    int x;
    
    
    
    do
    {
            cout << "Enter amount of rods to be dropped:"<< endl;
            cin >> num_rods;
    
           
            for (x=1; x<=num_rods; x++)
                triangles = triangles + get_tri(amt); 
                
             prob = tri_prob(num_rods, triangles, prob);
    
            cout << "The probability that "<< num_rods << " rods will form triangles is:" << 
            endl;
            cout << prob <<"%"<< endl;
            
     } while (num_rods!= SENTINEL);
     
  
      
    
    
        cout << "Have a Nice Day!"<< endl;
    
}

int get_tri(int& amt)
{
    float break1;         //first break point
    float break2;         //second break point
    float side1;        //first side
    float side2;        //second side
    float side3;        //third side
    int x;
    amt=0;
    
   
   for(x=0; x<200; x++) 
   {
       break1=((float)rand()/(float)RAND_MAX); //gets first breaking point
    break2= ((float)rand()/(float)RAND_MAX);// second breaking point
    
    if (break1>break2)
    {
        side1 = break1;         //determines side 1
        side2 = break1 - break2;        //determines side 2
        side3 = 1 - break2;     //determines side 3
    }
    else
    {
        side1 = break2;
        side2 = break2 - break1;
        side3 = 1 - break2;
    }
    
    
    
        if 
         (
         (side1 + side2) > side3 &&     //Determines whether a
         (side2 +  side3) > side1 &&    //triangle is formed from
         (side1 + side3) > side2        //breaking points
       )
    {
        amt += 1;
    
        
        return amt ;
    }
    else 
        return 0;
   }
 }

float tri_prob(int num_rods, int triangles, float& prob)
{
    prob = (triangles/num_rods)*100;
    
    return prob;
}
Last edited on
reading this program puts me on a trail of functions, it might be better to organize the program. I was able to write this using 49 lines, ill look into yours in more detail.


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
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iomanip>
bool isTri(double,double,double);
int main()
{
    srand((unsigned)time(NULL));                        //seed random
    double a,b,c,point1,point2,smaller_num,larger_num;  //declare vars
    int Trials,count,designated;
    Trials = count = designated = 0;                    //end
    std::cout.precision(3);                             //set precision
    std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
    std::cout.setf(std::ios_base::showpoint);           //end
    do{                                                 //while not 21
        std::cout << "Enter number of trials: ";
        std::cin >> designated;
        while(Trials != designated)                         ///glass rod breaking
        {
            point2 = static_cast<double>(rand()) / RAND_MAX; //point of break one
            point1 = static_cast<double>(rand()) / RAND_MAX; //point of break two
            if(point2>point1){                               //set coordinates
                larger_num = point2;
                smaller_num = point1;
            }else{
                larger_num = point1;
                smaller_num = point2;
                }
            a = smaller_num;                                //set parts of triangle equal to the break points
            b = larger_num - smaller_num;
            c = 1 - larger_num;
            if(isTri(a,b,c))                                //use IsTri function to determine if it is indeed a triangle
                count++;
            Trials++;
        }
        std::cout << "During the simulation of " << designated << " glass rods breaking,\n" << count
        << " became triangles. That is a probability of " << ((double)count / Trials) << std::endl;
    }while(designated != 21);
}
bool isTri(double a,double b,double c)
{
    if(a + b > c)               //tests if triangle or not
        if(a + c > b)
            if(b + c < a)
                return true;
    return false;
}
Last edited on
Hopefully I will be that good soon, but right now this is my best work, unfortunately like I was saying I can't get my probability right, i think it might be an issue with counting the triangles and returning the value. Oh and by the way your name fits how my life is right now perfect.
Hi djru,

Apart from better organisation of your program, as Need4Sleep has said, if you are having trouble with values of variables, then use the debugger to track these values. You can add variables to a watchlist if you are using an IDE. Command line debuggers like gdb on linux are harder to use, if you have to use this then Google for tutorials.

If you don't know how to use the debugger, put cout statements where you think there might be a problem, to see how the values change.

Just trying to point you towards what most programmers do - If you find out how to fix it yourself, then you will have a much better understanding of how it works, and will learn heaps more.

I can't remember, whether I mentioned in your other thread, about being careful about comparing floats. You should use some sort of EPSILON value. Google it.
I think I might be repeating some of what I said in your other topic, but I'll tell you everything I see is wrong (which may not be all that's wrong with it).

int get_tri(int&)

Not exactly anything wrong here, but it's pointless for you pass a variable to your get_tri function. You don't even use it. Or actually you do, but there's no reason to use it.

float tri_prob(int, int, float&)

Same as above, there's really no reason for you to be passing your prob variable to this function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
do
    {
            cout << "Enter amount of rods to be dropped:"<< endl;
            cin >> num_rods;
    
           
            for (x=1; x<=num_rods; x++)
                triangles = triangles + get_tri(amt); 
                
             prob = tri_prob(num_rods, triangles, prob);
    
            cout << "The probability that "<< num_rods << " rods will form triangles is:" << 
            endl;
            cout << prob <<"%"<< endl;
            
     } while (num_rods!= SENTINEL);


You never reset the triangles variable. That means if you decided to find the number multiple times, the triangle count from previous attempts is carried over.

You should also be putting the cin >> num_rods at the bottom of the loop. Otherwise, you're going to run through the code after you enter your sentinel value.

1
2
3
4
5
6
7
8
9
10
11
int get_tri(int& amt)
{
	for(x=0; x<200; x++)
	{
		//stuff
		if (something...)
			return amt;
		else
			return 0;
	}
}


What is the purpose of this loop? You have return statements in there. When the function returns, the loop would be terminated. You're never going to loop.

1
2
3
4
5
6
7
8
9
10
11
12
if (break1>break2)
    {
        side1 = break1;         //determines side 1
        side2 = break1 - break2;        //determines side 2
        side3 = 1 - break2;     //determines side 3
    }
    else
    {
        side1 = break2;
        side2 = break2 - break1;
        side3 = 1 - break2;
    }


Think about the values you're setting. The largest value should actually be the length of two sides, right???

1
2
3
4
5
6
float tri_prob(int num_rods, int triangles, float& prob)
{
    prob = (triangles/num_rods)*100;
    
    return prob;
}


Triangles and num_rods are both integers. That means the quotient is going to be an integer. Since triangles/num_rods is (probably) going to be less than 1, that becomes 0. And 0 * 100 is still 0.
Last edited on
I really appreciate all of the help, I finally got the program to run correctly. I ended up changing all of my data type int to data type float, and then I realized that everytime I put in a new number my percentage was getting higher and higher so I needed to reset my variable triangles=0 of course it would have took me twice as long to figure it out without dissecting everything everyone was telling me to look at. As for organization i'm sure it will get better as I go, I was just happy to have it running correctly. Here is the code I ended up with
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<iomanip>

using namespace std;
//Protocols
//Determining amount of triangles formed from rods
int get_tri(float&);
//Gets intial information for program
void get_info();
//Determines probability of triangles formed from amount
//of rods broken
float tri_prob(float, float, float&);

int main()
{
    srand(time(0));     //sets srand to pick random numbers
   
    //Title and purpose of program
    cout << "Broken Glass Simulator:"<< endl<<endl;
    cout << "This simulator determines how many triangles"<< endl;
    cout << "are formed from a set amount";
    cout << " of dropped glass rods"<<endl<<endl;
    cout << "Number 21 ends the program."<<endl<<endl;
    
    get_info();
    
}

void get_info()
{
    const int SENTINEL = 21;    // Sentinel value to exit program
    float num_rods;       //Number of glass rods dropped
    float triangles=0;  //amount of triangles formed
    float prob;         //probability of triangles formed
    float amt;
    int x;
    
    
    
    do
    {
            cout << "Enter amount of rods to be dropped:"<< endl;
            cin >> num_rods;
    
           
            for (x=1; x<=num_rods; x++)
                triangles = triangles + get_tri(amt); 
                
             prob = tri_prob(num_rods, triangles, prob);
    
            cout << "The probability that "<< num_rods << " rods will form triangles is:" << 
            endl;
            cout <<setprecision(3)<< prob <<"%"<< endl;
            
            triangles=0;
            
     } while (num_rods!= SENTINEL);
     
  
      
    
    
        cout << "Have a Nice Day!"<< endl;
    
}

int get_tri(float& amt)
{
    float break1;         //first break point
    float break2;         //second break point
    float side1;        //first side
    float side2;        //second side
    float side3;        //third side
    int x;
    amt=0;
    
   
   for(x=0; x<200; x++) 
   {
       break1=((float)rand()/(float)RAND_MAX); //gets first breaking point
    break2= ((float)rand()/(float)RAND_MAX);// second breaking point
    
    if (break1>break2)
    {
        side1 = break1;         //determines side 1
        side2 = break1 - break2;        //determines side 2
        side3 = 1 - break2;     //determines side 3
    }
    else
    {
        side1 = break2;
        side2 = break2 - break1;
        side3 = 1 - break2;
    }
    
    
    
        if 
         (
         (side1 + side2) > side3 &&     //Determines whether a
         (side2 +  side3) > side1 &&    //triangle is formed from
         (side1 + side3) > side2        //breaking points
       )
    {
        amt += 1;
    
        
        return amt ;
    }
    else 
        return 0;
   }
 }

float tri_prob(float num_rods, float triangles, float& prob)
{
    prob = (triangles/num_rods)*100;
    
    return prob;
}


Now on to the next project!!!
Topic archived. No new replies allowed.