programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Use Monte Carlo simulation calculate probability

qi zhou Wang
Greenhorn
Posts: 11
(Monte Carlo simulation) A square is divided into four smaller regions as shown below in (a). If you throw a dart into the square 1,000,000 times, what is the probability for a dart to fall into an odd-numbered region? Write a program to simulate the process and display the result.

How code for the region 3?
image.png

fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
you forget about writing code. You start by writing out in English (or whatever your natural language of choice is) how YOU would figure it out, with pencil and paper. If someone gave you the coordinates of (17,43), how would YOU know if it were inside that area or not?

Campbell Ritchie
Marshal
Posts: 56578
172
And welcome to the Ranch

qi zhou Wang
Greenhorn
Posts: 11
fred rosenberger wrote:you forget about writing code. You start by writing out in English (or whatever your natural language of choice is) how YOU would figure it out, with pencil and paper. If someone gave you the coordinates of (17,43), how would YOU know if it were inside that area or not?

Well, this is a unit circle.

Campbell Ritchie
Marshal
Posts: 56578
172
What's a unit circle? I can see squares but not circles.

Campbell Ritchie
Marshal
Posts: 56578
172
Work out how you are going to test whether a point is inside the area 3. It is quite simple.

qi zhou Wang
Greenhorn
Posts: 11
Campbell Ritchie wrote:Work out how you are going to test whether a point is inside the area 3. It is quite simple.

Can you teach me solve it?

Winston Gutkowski
Bartender
Posts: 10575
66
qi zhou Wang wrote:Well, this is a unit circle.

What's a circle got to do with it? You have rectangles, presumably of known dimensions. The only wrinkle is that the one that makes up regions 2 and 3 has a diagonal dividing it.

Tip: Think of those two regions as right-angle triangles, and your diagonal as the hypotenuse. Does that suggest anything to you?

Winston

qi zhou Wang
Greenhorn
Posts: 11
Campbell Ritchie wrote:What's a unit circle? I can see squares but not circles.

It's my fault!

qi zhou Wang
Greenhorn
Posts: 11
Winston Gutkowski wrote:
qi zhou Wang wrote:Well, this is a unit circle.

What's a circle got to do with it? You have rectangles, presumably of known dimensions. The only wrinkle is that the one that makes up regions 2 and 3 has a diagonal dividing it.

Tip: Think of those two regions as right-angle triangles, and your diagonal as the hypotenuse. Does that suggest anything to you?

Winston

It's a unit square, not a unit circle, my fault.

Winston Gutkowski
Bartender
Posts: 10575
66
qi zhou Wang wrote:A square is divided into four smaller regions as shown below in (a). If you throw a dart into the square 1,000,000 times, what is the probability for a dart to fall into an odd-numbered region? Write a program to simulate the process and display the result.

It's probably worth adding that there are actually two components to the problem:
1. The "what is the probabllity?" question.
2. The actual simulation.
and the two are quite separate.

Personally, I'd tackle each of them in turn and then publish "expected vs. actual" results in my simulation; but it's up to you.

Winston

Campbell Ritchie
Marshal
Posts: 56578
172
The question is actually badly worded; the probability of a randomly‑aimed missile which actually hits the field landing in an odd‑numbered area is independent of the number of missiles thrown. I don't think it says to work out the probability, but to run a simulation. But ”expected v actual” will look very good in the result.

Anyway: Assuming the area is a square 1×1, though it doesn't look like a square on the diagram, you have already worked out that x < 0 give you the left half. Now you simply have to work out how to identify area 3.

qi zhou Wang
Greenhorn
Posts: 11
Winston Gutkowski wrote:
qi zhou Wang wrote:A square is divided into four smaller regions as shown below in (a). If you throw a dart into the square 1,000,000 times, what is the probability for a dart to fall into an odd-numbered region? Write a program to simulate the process and display the result.

It's probably worth adding that there are actually two components to the problem:
1. The "what is the probabllity?" question.
2. The actual simulation.
and the two are quite separate.

Personally, I'd tackle each of them in turn and then publish "expected vs. actual" results in my simulation; but it's up to you.

Winston

I understand you stated, according to the figure can be learned that diagonal's slope is -1, but how to use slope represent region 3?

Campbell Ritchie
Marshal
Posts: 56578
172
You can use that slope to work out the permissible ranges for x and y.

qi zhou Wang
Greenhorn
Posts: 11
Campbell Ritchie wrote:The question is actually badly worded; the probability of a randomly‑aimed missile which actually hits the field landing in an odd‑numbered area is independent of the number of missiles thrown. I don't think it says to work out the probability, but to run a simulation. But ”expected v actual” will look very good in the result.

Anyway: Assuming the area is a square 1×1, though it doesn't look like a square on the diagram, you have already worked out that x < 0 give you the left half. Now you simply have to work out how to identify area 3.

Thanks to reply, it's just a exercise, throw dart not like launch missile, and you consider very carefully.

Campbell Ritchie
Marshal
Posts: 56578
172
Assuming the ◷ means origin = (0 0), and the other two corners of the △ are shown by the numbers, work out the x and y for all the little xs in the diagram. Can you see anything about those x and y values?

qi zhou Wang
Greenhorn
Posts: 11
Campbell Ritchie wrote:You can use that slope to work out the permissible ranges for x and y.

use formula y = mx + b?

Campbell Ritchie
Marshal
Posts: 56578
172
Yes, y = mx + b will help

Campbell Ritchie
Marshal
Posts: 56578
172
qi zhou Wang wrote: . . . throw dart not like launch missile, . . .
A dart is a very small missile

qi zhou Wang
Greenhorn
Posts: 11
Campbell Ritchie wrote:Yes, y = mx + b will help

qi zhou Wang
Greenhorn
Posts: 11
Campbell Ritchie wrote:
qi zhou Wang wrote: . . . throw dart not like launch missile, . . .
A dart is a very small missile

Ok, I agree with you.

Campbell Ritchie
Marshal
Posts: 56578
172
Go back to what Fred said:- paper before code.

qi zhou Wang
Greenhorn
Posts: 11
Campbell Ritchie wrote:Go back to what Fred said:- paper before code.

Thank you, before ask I'll think it.