• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA for Probability of forming triangle on breaking stick in 3 parts

AExcel

New Member
Hi guys,

I need help regarding writing a VBA macro for a question.

Question: If 1 m long stick is broken into 3 pieces. Find the probability that the 3 pieces will form a triangle.

If anyone of you could help me regarding this it will be great.
Thanks in advance :)
 
? I believe question is incomplete. At any rate, that's more of mathematics question than Excel question.

If you need help with VBA. You'll need to explain to us non mathematician, underlying calculation and logic, along with constraints.
 
? I believe question is incomplete. At any rate, that's more of mathematics question than Excel question.

If you need help with VBA. You'll need to explain to us non mathematician, underlying calculation and logic, along with constraints.

Hey Chihiro, I understand that it is more of a Mathematics puzzle, but the task here is to solve it through VBA. I can explain you the simple condition involved.

The condition for any three sides to form triangle is that sum of any two of them should be greater than the third one.

So let parts be x,y and 1- (x+y)

then constraints are

x+y>l-x-y ⇒ x+y> l/2.
x+l-x-y>y ⇒ y<l/2
y+l-x-y>x⇒ x<l/2 .

Also, to understand it more simply you can use this link.

https://www.quora.com/A-rod-is-brok...arts-can-be-arranged-to-form-a-triangle#!n=12

I am not able to understand, how to do it in Macro.
If you can guide, would be of great help.
Thanks.
 
Hmm, after reading through the link. I fail to understand why you'd need VBA for this.

There is only one possible solution and does not require iteration...
 
If you want to try to confirm this with iteration and random break points in the stick then you can do something like this:
Code:
Sub blah()
For i = 1 To 10000
  a = Rnd
  b = Rnd
  x = Application.Min(a, b)
  Z = 1 - Application.Max(a, b)
  y = 1 - x - Z
  'Debug.Print x + y + Z, x, y, Z
  If x + y > Z And x + Z > y And Z + y > x Then CanFormTriangle = CanFormTriangle + 1
  TestCount = TestCount + 1
Next i
MsgBox CanFormTriangle & "/" & TestCount & " = a " & Format(CanFormTriangle / TestCount, "0.000%") & " chance of forming a triangle"
End Sub
or if you want to go through in a more orderly fashion, something along the lines of:
Code:
Sub blah2()
Granularity = 1000
For i = 1 To Granularity - 2
  a = i / Granularity
  For j = i + 1 To Granularity - 1
    b = j / Granularity
    x = a
    y = b - a
    Z = 1 - b
    'Debug.Print x + y + Z, x, y, Z
    If x + y > Z And x + Z > y And Z + y > x Then CanFormTriangle = CanFormTriangle + 1
    TestCount = TestCount + 1
  Next j
Next i
MsgBox CanFormTriangle & "/" & TestCount & " = a " & Format(CanFormTriangle / TestCount, "0.000%") & " chance of forming a triangle"
End Sub
 
To be precise this type of problem cannot be "solved" using any standard program because the state space is continuous, not discrete. (It is however quite possible in the not too distant future from what I understand that AI may have advanced to be able to tackle such problems.)

If the problem is to approximate the answer the code provided above will come very close.
 
Back
Top