Random numbers
Data2viz offers several random numbers generators you can access using the RandomDistribution.*
factories.
To use these random numbers distributions, import the io.data2viz.random
dependency and use the
io.data2viz.random.*
import.
RandomDistribution.uniform
is the "standard" number generator.
The RandomDistribution.uniform(min, max)
generates random numbers using an uniform distribution.
Numbers generated are greater or equals to min
and less than max
.
Default min
is 0.0, default max
is 1.0, so default range is [0, 1[
import io.data2viz.color.*
import io.data2viz.geom.*
import io.data2viz.math.*
import io.data2viz.viz.*
import io.data2viz.random.*
fun main() {
viz {
size = size(620, 100)
(0..600 step 50).forEach {
text {
x = 10.0 + it
y = 94.0
textContent = "$it"
fill = Colors.Web.black
textAlign = textAlign(TextHAlign.MIDDLE, TextVAlign.MIDDLE)
}
}
//sampleStart
val generator = RandomDistribution.uniform(max = 600.0)
val groupCount = (0..30000).map { generator().toInt() }
.groupingBy { it }
.eachCount()
groupCount.forEach { valueCount ->
rect {
y = 85.0 - valueCount.value
x = 10.0 + valueCount.key
size = size(1, valueCount.value)
fill = Colors.Web.crimson
}
} //sampleEnd
}.bindRendererOnNewCanvas()
}
Other distributions
Normal
The RandomDistribution.normal(mu, sigma)
generates random numbers using a normal (Gaussian) distribution.
The expected value of the generated numbers is mu
(default 0.0), with the given standard
deviation sigma
(default 1.0).
import io.data2viz.color.*
import io.data2viz.geom.*
import io.data2viz.math.*
import io.data2viz.viz.*
import io.data2viz.random.*
fun main() {
viz {
size = size(620, 100)
(0..600 step 50).forEach {
text {
x = 10.0 + it
y = 94.0
textContent = "$it"
fill = Colors.Web.black
textAlign = textAlign(TextHAlign.MIDDLE, TextVAlign.MIDDLE)
}
}
//sampleStart
val generator = RandomDistribution.normal(300.0, 50.0)
val groupCount = (0..8000).map { generator().toInt() }
.groupingBy { it }
.eachCount()
groupCount.forEach { valueCount ->
rect {
y = 85.0 - valueCount.value
x = 10.0 + valueCount.key
size = size(1, valueCount.value)
fill = Colors.Web.crimson
}
} //sampleEnd
}.bindRendererOnNewCanvas()
}
Log-Normal
The RandomDistribution.logNormal(mu, sigma)
generates random numbers using a log-normal distribution.
The expected value of the random variable’s natural logarithm is mu
(defaults to 0.0), with the given
standard deviation sigma
(defaults to 1.0)
import io.data2viz.color.*
import io.data2viz.geom.*
import io.data2viz.math.*
import io.data2viz.viz.*
import io.data2viz.random.*
fun main() {
viz {
size = size(620, 100)
(0..600 step 60).forEach {
text {
x = 10.0 + it
y = 94.0
textContent = "${(it / 3.0).toInt() / 100.0}"
fill = Colors.Web.black
textAlign = textAlign(TextHAlign.MIDDLE, TextVAlign.MIDDLE)
}
}
//sampleStart
val generator = RandomDistribution.logNormal(sigma = 0.25)
val groupCount = (0..8000).map { generator() }
.groupingBy { (it * 300).toInt() }
.eachCount()
groupCount.forEach { valueCount ->
rect {
y = 85.0 - valueCount.value
x = 10.0 + valueCount.key
size = size(1, valueCount.value)
fill = Colors.Web.crimson
}
} //sampleEnd
}.bindRendererOnNewCanvas()
}
Exponential
The RandomDistribution.exponential(lambda)
generates random numbers using an exponential distribution.
This generates random numbers with an exponential distribution with the rate lambda
(defaults to 1.0)
equivalent to time between events in a Poisson process with a mean of 1 / lambda.
import io.data2viz.color.*
import io.data2viz.geom.*
import io.data2viz.math.*
import io.data2viz.viz.*
import io.data2viz.random.*
fun main() {
viz {
size = size(620, 100)
(0..600 step 60).forEach {
text {
x = 10.0 + it
y = 94.0
textContent = "${(it / 60.0).toInt()}"
fill = Colors.Web.black
textAlign = textAlign(TextHAlign.MIDDLE, TextVAlign.MIDDLE)
}
}
//sampleStart
val generator = RandomDistribution.exponential()
val groupCount = (0..4000).map { generator() }
.groupingBy { (it * 60).toInt() }
.eachCount()
groupCount.forEach { valueCount ->
rect {
y = 85.0 - valueCount.value
x = 10.0 + valueCount.key
size = size(1, valueCount.value)
fill = Colors.Web.crimson
}
} //sampleEnd
}.bindRendererOnNewCanvas()
}
Irwin-Hall
The RandomDistribution.irwinHall(n)
generates random numbers using an Irwin-Hall distribution
with n
independent variables.
import io.data2viz.color.*
import io.data2viz.geom.*
import io.data2viz.math.*
import io.data2viz.viz.*
import io.data2viz.random.*
fun main() {
viz {
size = size(620, 100)
(0..600 step 60).forEach {
text {
x = 10.0 + it
y = 94.0
textContent = "${(it / 60.0).toInt()}"
fill = Colors.Web.black
textAlign = textAlign(TextHAlign.MIDDLE, TextVAlign.MIDDLE)
}
}
//sampleStart
val generator = RandomDistribution.irwinHall(8.0)
val groupCount = (0..8000).map { generator() }
.groupingBy { (it * 60).toInt() }
.eachCount()
groupCount.forEach { valueCount ->
rect {
y = 85.0 - valueCount.value
x = 10.0 + valueCount.key
size = size(1, valueCount.value)
fill = Colors.Web.crimson
}
} //sampleEnd
}.bindRendererOnNewCanvas()
}
Bates
The RandomDistribution.bates(n)
generates random numbers using a Bates distribution
with n
independent variables (1.0 by default).
import io.data2viz.color.*
import io.data2viz.geom.*
import io.data2viz.math.*
import io.data2viz.viz.*
import io.data2viz.random.*
fun main() {
viz {
size = size(620, 100)
(0..600 step 60).forEach {
text {
x = 10.0 + it
y = 94.0
textContent = "${(it / 6.0).toInt() / 100.0}"
fill = Colors.Web.black
textAlign = textAlign(TextHAlign.MIDDLE, TextVAlign.MIDDLE)
}
}
//sampleStart
val generator = RandomDistribution.bates(10.0)
val groupCount = (0..8000).map { generator() }
.groupingBy { (it * 600).toInt() }
.eachCount()
groupCount.forEach { valueCount ->
rect {
y = 85.0 - valueCount.value
x = 10.0 + valueCount.key
size = size(1, valueCount.value)
fill = Colors.Web.crimson
}
} //sampleEnd
}.bindRendererOnNewCanvas()
}