This week's book giveaway is in the Jobs Discussion forum. We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line! See this thread for details.

(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.

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

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?

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

posted

0

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

posted

0

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?

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: 43423

32

posted

0

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

posted

0

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: 43423

32

posted

0

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

posted

0

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: 43423

32

posted

0

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

posted

0

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: 43423

32

posted

0

Yes, y = mx + b will help

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43423

32

posted

0

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

posted

0

Campbell Ritchie wrote:Yes, y = mx + b will help

qi zhou Wang
Greenhorn

Joined: Dec 05, 2013
Posts: 11

posted

0

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: 43423

32

posted

0

Go back to what Fred said:- paper before code.

qi zhou Wang
Greenhorn

Joined: Dec 05, 2013
Posts: 11

posted

0

Campbell Ritchie wrote:Go back to what Fred said:- paper before code.