aspose file tools*
The moose likes Beginning Java and the fly likes Use Monte Carlo simulation calculate probability Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Use Monte Carlo simulation calculate probability" Watch "Use Monte Carlo simulation calculate probability" New topic
Author

Use Monte Carlo simulation calculate probability

qi zhou Wang
Greenhorn

Joined: Dec 05, 2013
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

Joined: Oct 02, 2003
Posts: 11417
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
And welcome to the Ranch
qi zhou Wang
Greenhorn

Joined: Dec 05, 2013
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
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
What's a unit circle? I can see squares but not circles.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Work out how you are going to test whether a point is inside the area 3. It is quite simple.
qi zhou Wang
Greenhorn

Joined: Dec 05, 2013
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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Joined: Dec 05, 2013
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

Joined: Dec 05, 2013
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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Joined: Oct 13, 2005
Posts: 39409
    
  28
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

Joined: Dec 05, 2013
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
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
You can use that slope to work out the permissible ranges for x and y.
qi zhou Wang
Greenhorn

Joined: Dec 05, 2013
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
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
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

Joined: Dec 05, 2013
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
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Yes, y = mx + b will help
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
qi zhou Wang wrote: . . . throw dart not like launch missile, . . .
A dart is a very small missile
qi zhou Wang
Greenhorn

Joined: Dec 05, 2013
Posts: 11
Campbell Ritchie wrote:Yes, y = mx + b will help

qi zhou Wang
Greenhorn

Joined: Dec 05, 2013
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
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Go back to what Fred said:- paper before code.
qi zhou Wang
Greenhorn

Joined: Dec 05, 2013
Posts: 11
Campbell Ritchie wrote:Go back to what Fred said:- paper before code.

Thank you, before ask I'll think it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use Monte Carlo simulation calculate probability