Random numbers: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|Scala}}: migrate to Scala 2.13) |
||
Line 2,145: | Line 2,145: | ||
<lang scala>List.fill(1000)(1.0 + 0.5 * scala.util.Random.nextGaussian)</lang> |
<lang scala>List.fill(1000)(1.0 + 0.5 * scala.util.Random.nextGaussian)</lang> |
||
===Academic=== |
===Academic=== |
||
<lang scala> |
<lang scala> |
||
object RandomNumbers extends App { |
|||
⚫ | |||
val distribution: LazyList[Double] = { |
|||
def normalDistribution(a: Double): Stream[Double] = a #:: normalDistribution(randomNormal) |
|||
⚫ | |||
normalDistribution(randomNormal) |
def normalDistribution(a: Double): LazyList[Double] = a #:: normalDistribution(randomNormal) |
||
⚫ | |||
normalDistribution(randomNormal) |
|||
⚫ | |||
} |
|||
⚫ | |||
*/ |
|||
⚫ | |||
⚫ | |||
*/ |
|||
def calcAvgAndStddev[T](ts: Iterable[T])(implicit num: Fractional[T]): (T, Double) = { |
def calcAvgAndStddev[T](ts: Iterable[T])(implicit num: Fractional[T]): (T, Double) = { |
||
val mean: T = |
val mean: T = |
||
Line 2,161: | Line 2,164: | ||
// Root of mean diffs |
// Root of mean diffs |
||
val stdDev = sqrt(ts.map { x => |
val stdDev = Math.sqrt(ts.map { x => |
||
val diff = num.toDouble(num.minus(x, mean)) |
val diff = num.toDouble(num.minus(x, mean)) |
||
diff * diff |
diff * diff |
||
Line 2,169: | Line 2,172: | ||
} |
} |
||
println(calcAvgAndStddev( |
println(calcAvgAndStddev(distribution.take(1000))) // e.g. (1.0061433267806525,0.5291834867560893) |
||
⚫ | |||
</lang> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |