Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use Monte Carlo simulation calculate probability

 
qi zhou Wang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
[Thumbnail for image.png]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
qi zhou Wang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's a unit circle? I can see squares but not circles.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use that slope to work out the permissible ranges for x and y.
 
qi zhou Wang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, y = mx + b will help
 
Campbell Ritchie
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
qi zhou Wang wrote: . . . throw dart not like launch missile, . . .
A dart is a very small missile
 
qi zhou Wang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Yes, y = mx + b will help

 
qi zhou Wang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go back to what Fred said:- paper before code.
 
qi zhou Wang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Go back to what Fred said:- paper before code.

Thank you, before ask I'll think it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic