From 5d2dd7db439e2fb399cea43375bbc14b36fda52e Mon Sep 17 00:00:00 2001 From: davidontop Date: Tue, 24 Sep 2024 21:08:15 +0200 Subject: [PATCH] Initial commit --- .gitignore | 47 +++++++++++++++++++ LICENSE | 26 ++++++++++ build.sbt | 14 ++++++ project/build.properties | 1 + project/plugins.sbt | 1 + .../top/davidon/montecarlo/main/Main.scala | 37 +++++++++++++++ 6 files changed, 126 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 build.sbt create mode 100644 project/build.properties create mode 100644 project/plugins.sbt create mode 100644 src/main/scala/top/davidon/montecarlo/main/Main.scala diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bf3788d --- /dev/null +++ b/.gitignore @@ -0,0 +1,47 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store + +### Scala ### +.bsp/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a2319d9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,26 @@ + GLWTS(Good Luck With That Shit) Public License + Copyright (c) Every-fucking-one, except the Author + +Everyone is permitted to copy, distribute, modify, merge, sell, publish, +sublicense or whatever the fuck they want with this software but at their +OWN RISK. + + Preamble + +The author has absolutely no fucking clue what the code in this project +does. It might just fucking work or not, there is no third option. + + + GOOD LUCK WITH THAT SHIT PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION, AND MODIFICATION + + 0. You just DO WHATEVER THE FUCK YOU WANT TO as long as you NEVER LEAVE +A FUCKING TRACE TO TRACK THE AUTHOR of the original product to blame for +or hold responsible. + +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + +Good luck and Godspeed. \ No newline at end of file diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..b5cfe9c --- /dev/null +++ b/build.sbt @@ -0,0 +1,14 @@ +ThisBuild / version := "0.1.0" +ThisBuild / organization := "top.davidon" +ThisBuild / licenses ++= Seq( + "GLWTS" -> url("https://github.com/me-shaon/GLWTPL/blob/master/NSFW_LICENSE"), +) + +ThisBuild / scalaVersion := "3.5.1" + + +lazy val root = (project in file(".")) + .settings( + name := "monteCarlo", + assembly / mainClass := Some("top.davidon.montecarlo.main.Main"), + ) diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..23f7d97 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version = 1.10.2 diff --git a/project/plugins.sbt b/project/plugins.sbt new file mode 100644 index 0000000..5347652 --- /dev/null +++ b/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.2.0") \ No newline at end of file diff --git a/src/main/scala/top/davidon/montecarlo/main/Main.scala b/src/main/scala/top/davidon/montecarlo/main/Main.scala new file mode 100644 index 0000000..7bc80be --- /dev/null +++ b/src/main/scala/top/davidon/montecarlo/main/Main.scala @@ -0,0 +1,37 @@ +package top.davidon.montecarlo.main + +import java.util.concurrent.atomic.AtomicLong +import scala.concurrent.Future +import scala.concurrent.ExecutionContext.Implicits.global + +@main def main(iterCount: Long, batchSize: Int): Unit = { + MonteCarlo(iterCount, batchSize) +} + +class MonteCarlo(var iterCount: Long, val batchSize: Int) { + var sp = AtomicLong(0L) + var cp = AtomicLong(0L) + + { + while iterCount > 0 do { + val bs = if (iterCount < batchSize) iterCount.toInt else batchSize + val futures = for (i <- 0 until bs) yield calc() + Future.sequence(futures).map(_ => ()) + iterCount -= bs + println(s"iterCount = $iterCount") + } + val pi = (4.0*cp.get())/sp.get() + println(s"pi = $pi") + } + + def calc(): Future[Unit] = Future { + val px = Math.random()*2-1 + val py = Math.random()*2-1 + + val d = px*px + py*py + if (d < 1) { + cp.addAndGet(1) + } + sp.addAndGet(1) + } +}