Initial commit

This commit is contained in:
DavidOnTop 2024-09-24 21:08:15 +02:00
commit 5d2dd7db43
No known key found for this signature in database
GPG key ID: 5D05538A45D5149F
6 changed files with 126 additions and 0 deletions

47
.gitignore vendored Normal file
View file

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

26
LICENSE Normal file
View file

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

14
build.sbt Normal file
View file

@ -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"),
)

1
project/build.properties Normal file
View file

@ -0,0 +1 @@
sbt.version = 1.10.2

1
project/plugins.sbt Normal file
View file

@ -0,0 +1 @@
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.2.0")

View file

@ -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)
}
}