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

Published on 12 May 2024 09:40 PM

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

Source code in the crystal programming language

n, mean, sd, tau = 1000, 1, 0.5, (2 * Math::PI)
array = Array.new(n) { mean + sd * Math.sqrt(-2 * Math.log(rand)) * Math.cos(tau * rand) }

mean = array.sum / array.size
standev = Math.sqrt( array.sum{ |x| (x - mean) ** 2 } / array.size )
puts "mean = #{mean}, standard deviation = #{standev}"


  

You may also check:How to resolve the algorithm First perfect square in base n with n unique digits step by step in the Julia programming language
You may also check:How to resolve the algorithm Write entire file step by step in the BASIC programming language
You may also check:How to resolve the algorithm Harshad or Niven series step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Object serialization step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm GUI enabling/disabling of controls step by step in the Kotlin programming language