Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 6, 2023 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/1ef83ab380b24a8d6595de09ff7a4bd5 to your computer and use it in GitHub Desktop.
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/70facf963483b22c7863d7d20c0b29fb68d219e0
// 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.2.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