How to resolve the algorithm Random numbers step by step in the Lobster programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Random numbers step by step in the Lobster programming language

Table of Contents

Problem Statement

Generate a collection filled with   1000   normally distributed random (or pseudo-random) numbers with a mean of   1.0   and a   standard deviation   of   0.5 Many libraries only generate uniformly distributed random numbers. If so, you may use one of these algorithms.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Random numbers step by step in the Lobster programming language

Source code in the lobster programming language

let mean = 1.0
let stdv = 0.5
let count = 1000

// stats computes a running mean and variance 
// See Knuth TAOCP vol 2, 3rd edition, page 232

def stats(xs: [float]) -> float, float: // variance, mean
    var M = xs[0]
    var S = 0.0
    var n = 1.0
    for(xs.length - 1) i:
        let x = xs[i + 1]
        n = n + 1.0
        let mm = (x - M)
        M += mm / n
        S += mm * (x - M)
    return (if n > 0.0: S / n else: 0.0), M
 
def test_random_normal() -> [float]:
    rnd_seed(floor(seconds_elapsed() * 1000000))
    let r = vector_reserve(typeof return, count)
    for (count):
        r.push(rnd_gaussian() * stdv + mean)
    let cvar, cmean = stats(r)
    let cstdv = sqrt(cvar)
    print concat_string(["Mean: ", string(cmean), ", Std.Deviation: ", string(cstdv)], "")

test_random_normal()

  

You may also check:How to resolve the algorithm Attractive numbers step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the Rust programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the jq programming language
You may also check:How to resolve the algorithm Date format step by step in the AWK programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Phix programming language