qi zhou Wang

Greenhorn

Posts: 11

posted 3 years ago

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

How code for the region 3?

image.png

posted 3 years ago

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

qi zhou Wang

Greenhorn

Posts: 11

posted 3 years ago

Well, this is a unit circle.

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

161

Campbell Ritchie

Marshal

Posts: 55681

161

qi zhou Wang

Greenhorn

Posts: 11

posted 3 years ago

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

qi zhou Wang

Greenhorn

Posts: 11

posted 3 years ago

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

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.

posted 3 years ago

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

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

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Campbell Ritchie

Marshal

Posts: 55681

161

posted 3 years ago

The question is actually badly worded; the probability of a

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

__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

posted 3 years ago

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?

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

161

qi zhou Wang

Greenhorn

Posts: 11

posted 3 years ago

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

Campbell Ritchie wrote:The question is actually badly worded; the probability of arandomly‑aimedmissile whichactually hitsthe 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 ”expectedvactual” 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 thatx < 0give 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: 55681

161

qi zhou Wang

Greenhorn

Posts: 11

Campbell Ritchie

Marshal

Posts: 55681

161

qi zhou Wang

Greenhorn

Posts: 11

qi zhou Wang

Greenhorn

Posts: 11

Campbell Ritchie

Marshal

Posts: 55681

161

It is sorta covered in the JavaRanch Style Guide. |