| 1 | (******************************************************************************* |
|---|
| 2 | Copyright 2009 Sun Microsystems, Inc., |
|---|
| 3 | 4150 Network Circle, Santa Clara, California 95054, U.S.A. |
|---|
| 4 | All rights reserved. |
|---|
| 5 | |
|---|
| 6 | U.S. Government Rights - Commercial software. |
|---|
| 7 | Government users are subject to the Sun Microsystems, Inc. standard |
|---|
| 8 | license agreement and applicable provisions of the FAR and its supplements. |
|---|
| 9 | |
|---|
| 10 | Use is subject to license terms. |
|---|
| 11 | |
|---|
| 12 | This distribution may include materials developed by third parties. |
|---|
| 13 | |
|---|
| 14 | Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered |
|---|
| 15 | trademarks of Sun Microsystems, Inc. in the U.S. and other countries. |
|---|
| 16 | ******************************************************************************) |
|---|
| 17 | |
|---|
| 18 | component buffons |
|---|
| 19 | |
|---|
| 20 | export Executable |
|---|
| 21 | |
|---|
| 22 | needleLength = 20 |
|---|
| 23 | numRows = 10 |
|---|
| 24 | tableHeight = needleLength numRows |
|---|
| 25 | |
|---|
| 26 | run():()=do |
|---|
| 27 | var hits : RR64 := 0.0 |
|---|
| 28 | var n : RR64 := 0.0 |
|---|
| 29 | |
|---|
| 30 | println("Starting parallel Buffons") |
|---|
| 31 | recordTime(6.0) |
|---|
| 32 | for i <- 1#3000 do |
|---|
| 33 | delta_X = random(2.0) - 1 |
|---|
| 34 | delta_Y = random(2.0) - 1 |
|---|
| 35 | rsq = delta_X^2 + delta_Y^2 |
|---|
| 36 | if 0 < rsq < 1 then |
|---|
| 37 | y1 = tableHeight random(1.0) |
|---|
| 38 | y2 = y1 + needleLength (delta_Y / SQRT rsq) |
|---|
| 39 | (y_L, y_H) = (y1 MIN y2, y1 MAX y2) |
|---|
| 40 | if |/ y_L/needleLength \| = |\ y_H/needleLength /| then |
|---|
| 41 | atomic do hits += 1.0 end |
|---|
| 42 | end |
|---|
| 43 | atomic do n += 1.0 end |
|---|
| 44 | end |
|---|
| 45 | end |
|---|
| 46 | probability = hits/n |
|---|
| 47 | pi_est = 2.0/probability |
|---|
| 48 | printTime(6.0) |
|---|
| 49 | println("") |
|---|
| 50 | print("estimated Pi = ") |
|---|
| 51 | println(pi_est) |
|---|
| 52 | end |
|---|
| 53 | end |
|---|