Last active
May 25, 2024 08:39
-
-
Save dacr/1ef83ab380b24a8d6595de09ff7a4bd5 to your computer and use it in GitHub Desktop.
plotting random series variations as an attempt to illustrate mandelbrot (un)wild chance / published by https://github.com/dacr/code-examples-manager #b308d13a-e09b-43d8-a75d-3be06d3f1504/57fb96e01b8edd7d7af16ea503467c2c96f97111
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : plotting random series variations as an attempt to illustrate mandelbrot (un)wild chance | |
// keywords : scala, chart, plotly, timeseries, rng, random, mandelbrot | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : b308d13a-e09b-43d8-a75d-3be06d3f1504 | |
// created-on : 2020-11-27T07:49:48Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc' | |
// -- https://github.com/alexarchambault/plotly-scala | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "org.plotly-scala:plotly-render_2.13:0.8.4" | |
// --------------------- | |
import plotly._, element._, layout._, Plotly._ | |
val rand = new java.util.Random() | |
// ------------------------------------------------------------------------------------ | |
val randgenSecureRNG = () => rand.nextInt(101)-50 | |
// ------------------------------------------------------------------------------------ | |
class XORShift32(val seed: Long = System.currentTimeMillis()) { | |
private var cseed = seed | |
private def randomInt = { | |
var x = cseed | |
x ^= (x << 13) | |
x ^= (x >>> 17) | |
x ^= (x << 5) | |
cseed = x | |
x | |
} | |
final def nextInt(bound: Int):Int = ((randomInt & 0x7fffff) % bound).toInt | |
} | |
val randgenXorShiftRNG = { | |
val rng = new XORShift32() | |
() => rng.nextInt(101)-50 | |
} | |
// ------------------------------------------------------------------------------------ | |
class BasicFastRandom(seed: Long = System.currentTimeMillis()) { | |
private var g_seed = (seed >> 16 & 0x7FFFFFFF).toInt | |
final def nextBoolean = nextInt(2) != 0 | |
final def nextInt(n: Int) = { | |
g_seed = 214013 * g_seed + 2531011 | |
((g_seed >> 16) & 0x7FFF) % n | |
} | |
} | |
val basicFastRNG = { | |
val rng = new BasicFastRandom() | |
() => rng.nextInt(101)-50 | |
} | |
// ------------------------------------------------------------------------------------ | |
val start = 0 | |
val count = 500000 | |
val xs = (0 until count) | |
val ysSecureRNG = xs.scanLeft(start){case (a,b) => a+randgenSecureRNG()} | |
val ysXorShiftRNG = xs.scanLeft(start){case (a,b) => a+randgenXorShiftRNG()} | |
val ysBasicFastRNG = xs.scanLeft(start){case (a,b) => a+basicFastRNG()} | |
val data = Seq( | |
Scatter(xs, ysSecureRNG).withName("secureRNG"), | |
Scatter(xs, ysXorShiftRNG).withName("XorShiftRNG"), | |
Scatter(xs, ysBasicFastRNG).withName("basicFastRNG"), | |
)//.map(_.withFill(Fill.ToNextY)) | |
val myLayout = | |
Layout() | |
.withTitle("MyTimeSeries") | |
plot("plot.html", data, myLayout) | |
println("It should even open the default browser with that chart !") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment