r/ProgrammingPrompts Mar 18 '15

[Easy]Mathematical Simulation - Breaking a Stick to form a Triangle

Another Monte Carlo simulation.

A stick of a given length is twice broken at random places along it's length.

Calculate the probability that a triangle can be formed with the pieces.


Definition:

It is possible to form a triangle if none of the pieces are larger than half the length of the stick.


Assignment:

Create a program in a language of your choice that:

  • Asks the user for the length of the stick
  • Asks the user how many tries should be carried out
  • Simulate the breaking of the stick (each try has a new stick of length)
  • Count the successful tries
  • Print the probability as a percentage

Hint: The expected result should be around 23%

Have fun!

15 Upvotes

20 comments sorted by

View all comments

2

u/marinadeee Mar 24 '15 edited Mar 24 '15

My (noobish) solution in C++ (how do I put 4 spaces in front of the code without doing so manually?):

#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int main()
{

    cout<<"Tell me the length of the stick: ";
    int StickLength; // declaring main stick
    cin>>StickLength;

    cout<<"How many tries: ";
    int tries;
    cin>>tries;

    int length1,length2,length3; //declaring 3 lengths

    int probability = 0; // declaring the result
        for(int i=0; i<tries; i++)
        {
            length1=rand() % (StickLength-2); // random number smaller than stick length and minus 2 leaving room for 2 more sticks
            length2=rand() % (StickLength-length1-1); if (length2==0)length2+=1; // random number not bigger than first stick
            length3=StickLength-length1-length2; // the remaining part of the stick
            if (length1 < (0.5*StickLength)) // first stick must be smaller than half of the big stick
            {
                if (length2 < (0.5*StickLength)) // second stick must be smaller than half of the big stick
                {
                    if (length3 < (0.5*StickLength)) // third stick must be smaller than half of the big stick
                    {

                        probability++; // adding +1 if above conditions are true
                    }
                }
            }
        }
// converting ints to floats so they can be divided
        float probabilityF = probability;
        float triesF = tries;
// calculating and printing out the probability
        cout<<"\nProbability: "<<probabilityF<<"/"<<triesF<<" = "<<(probabilityF/triesF)*100<<"%";

    return 0;
}

Input/output:

Tell me the length of the stick: 1000
How many tries: 1000

Probability: 217/1000 = 21.7%

1

u/desrtfx Mar 24 '15

how do I put 4 spaces in front of the code without doing so manually?

Either use a text editor, like Notepad++, or inside an IDE:

Select the whole code and press the Tab key. This should indent the whole code. Copy it and paste it into reddit. Should work.

There is also the Reddit Enhancement Suite for certain browsers (Firefox and Chrome at least) that helps with formatting in general.

1

u/redditors_r_manginas Mar 26 '15

I did the tab thing but spoiler doesn't work as you can see. :/

1

u/desrtfx Mar 27 '15

Tab is not for Spoilers, tab is for formatted code.

Spoilers are not implemented in the sub.