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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Random numbers step by step in the Picat 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 Picat programming language

Source code in the picat programming language

main =>
   _ = random2(), % random seed
   G = [gaussian_dist(1,0.5) : _ in 1..1000],
   println(first_10=G[1..10]),
   println([mean=avg(G),stdev=stdev(G)]),
   nl.

% Gaussian (Normal) distribution, Box-Muller algorithm
gaussian01() = Y =>
    U = frand(0,1),
    V = frand(0,1),
    Y = sqrt(-2*log(U))*sin(2*math.pi*V).

gaussian_dist(Mean,Stdev) = Mean + (gaussian01() * Stdev).

% Variance of Xs
variance(Xs) = Variance =>
    Mu = avg(Xs),
    N  = Xs.len,
    Variance = sum([ (X-Mu)**2 : X in Xs ]) / N.

% Standard deviation
stdev(Xs) = sqrt(variance(Xs)).

  

You may also check:How to resolve the algorithm Babbage problem step by step in the Dart programming language
You may also check:How to resolve the algorithm Combinations step by step in the Lua programming language
You may also check:How to resolve the algorithm Loops/Nested step by step in the MOO programming language
You may also check:How to resolve the algorithm Count in factors step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Call a function step by step in the CoffeeScript programming language