Random numbers: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(→‎{{header|Scala}}: migrate to Scala 2.13)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 41:
end loop;
end Normal_Random;</lang>
 
=={{header|ALGOL 68}}==
{{trans|C}}
Line 83 ⟶ 84:
}
Return Y
}</lang>
 
=={{header|AWK}}==
Line 484 ⟶ 485:
Standard Deviation: 0.49042787564453988
</pre>
 
=={{header|Elena}}==
{{trans|C#}}
Line 665 ⟶ 667:
s[i] = 1 + 0.5 * RandomNormal()
end for</lang>
 
=={{header|Frink}}==
<lang frink>
a = new array
for i = 1 to 1000
a.push[randomGaussian[1, 0.5]]
</lang>
 
=={{header|F_Sharp|F#}}==
Line 700 ⟶ 695:
1.403719877; 0.5765950249; 1.275206565; 0.6292054813; 1.525562798;
0.6224640457; 0.8524078517; 0.7646595627; 0.6799834691; 0.773111053; ...]
</pre> =={{header|F_Sharp|F#}}==
</pre>
<lang fsharp>let gaussianRand count =
let o = new System.Random()
let pi = System.Math.PI
let gaussrnd =
(fun _ -> 1. + 0.5 * sqrt(-2. * log(o.NextDouble())) * cos(2. * pi * o.NextDouble()))
[ for i in {0 .. (int count)} -> gaussrnd() ]</lang>
 
=={{header|Factor}}==
<lang factor>USING: random ;
Line 817 ⟶ 819:
Standard Deviation = 0.503373
</pre>
 
=={{header|Free Pascal}}==
Free Pascal provides the '''randg''' function in the RTL math unit that produces Gaussian-distributed random numbers with the Box-Müller algorithm.
 
<lang pascal>
function randg(mean,stddev: float): float;
</lang>
 
=={{header|FreeBASIC}}==
Line 862 ⟶ 871:
</pre>
 
=={{header|Free PascalFrink}}==
<lang frink>
Free Pascal provides the '''randg''' function in the RTL math unit that produces Gaussian-distributed random numbers with the Box-Müller algorithm.
a = new array
for i = 1 to 1000
a.push[randomGaussian[1, 0.5]]
</lang>
 
=={{header|FutureBasic}}==
<lang pascal>
Note: To generate the random number, rather than using FB's native "rnd" function, this code wraps C code into the RandomZeroToOne function.
function randg(mean,stddev: float): float;
</lang futurebasic>
include "ConsoleWindow"
 
local fn RandomZeroToOne as double
dim as double result
BeginCCode
result = (double)( (rand() % 100000 ) * 0.00001 );
EndC
end fn = result
 
local fn RandomGaussian as double
dim as double r
 
r = fn RandomZeroToOne
end fn = 1 + .5 * ( sqr( -2 * log(r) ) * cos( 2 * pi * r ) )
 
dim as long i
dim as double mean, std, a(1000)
 
for i = 1 to 1000
a(i) = fn RandomGaussian
mean += a(i)
next
mean = mean / 1000
 
for i = 1 to 1000
=={{header|F_Sharp|F#}}==
std += ( a(i) - mean )^2
<lang fsharp>let gaussianRand count =
next
let o = new System.Random()
std = std / 1000
let pi = System.Math.PI
 
let gaussrnd =
print " Average:"; mean
(fun _ -> 1. + 0.5 * sqrt(-2. * log(o.NextDouble())) * cos(2. * pi * o.NextDouble()))
print "Standard Deviation:"; std
[ for i in {0 .. (int count)} -> gaussrnd() ]</lang>
</lang>
Output:
<pre>
Average: 1.0258434498
Standard Deviation: 0.2771047023
</pre>
 
=={{header|Go}}==
Line 941 ⟶ 983:
***
**
</pre>
 
=={{header|FutureBasic}}==
Note: To generate the random number, rather than using FB's native "rnd" function, this code wraps C code into the RandomZeroToOne function.
<lang futurebasic>
include "ConsoleWindow"
 
local fn RandomZeroToOne as double
dim as double result
BeginCCode
result = (double)( (rand() % 100000 ) * 0.00001 );
EndC
end fn = result
 
local fn RandomGaussian as double
dim as double r
 
r = fn RandomZeroToOne
end fn = 1 + .5 * ( sqr( -2 * log(r) ) * cos( 2 * pi * r ) )
 
dim as long i
dim as double mean, std, a(1000)
 
for i = 1 to 1000
a(i) = fn RandomGaussian
mean += a(i)
next
mean = mean / 1000
 
for i = 1 to 1000
std += ( a(i) - mean )^2
next
std = std / 1000
 
print " Average:"; mean
print "Standard Deviation:"; std
</lang>
Output:
<pre>
Average: 1.0258434498
Standard Deviation: 0.2771047023
</pre>
 
Line 1,163 ⟶ 1,164:
a( i) =mean +sd *( sqr( -2 * log( rnd( 0))) * cos( 2 * pi * rnd( 0)))
next i</lang>
 
=={{header|Logo}}==
{{works with|UCB Logo}}
The earliest Logos only have a RANDOM function for picking a random non-negative integer. Many modern Logos have floating point random generators built-in.
<lang logo>to random.float ; 0..1
localmake "max.int lshift -1 -1
output quotient random :max.int :max.int
end
 
to random.gaussian
output product cos random 360 sqrt -2 / ln random.float
end
 
make "randoms cascade 1000 [fput random.gaussian / 2 + 1 ?] []</lang>
 
=={{header|Lingo}}==
Line 1,223 ⟶ 1,210:
test_random_normal()
</lang>
 
=={{header|Logo}}==
{{works with|UCB Logo}}
The earliest Logos only have a RANDOM function for picking a random non-negative integer. Many modern Logos have floating point random generators built-in.
<lang logo>to random.float ; 0..1
localmake "max.int lshift -1 -1
output quotient random :max.int :max.int
end
 
to random.gaussian
output product cos random 360 sqrt -2 / ln random.float
end
 
make "randoms cascade 1000 [fput random.gaussian / 2 + 1 ?] []</lang>
 
=={{header|Lua}}==
Line 1,741 ⟶ 1,742:
1 + 0.5 * sqrt(-2 * log rand) * cos(2 * $PI * rand)
} 1..1000;</lang>
 
=={{header|Perl 6}}==
{{works with|Rakudo|#22 "Thousand Oaks"}}
 
<lang perl6>sub randnorm ($mean, $stddev) {
$mean + $stddev * sqrt(-2 * log rand) * cos(2 * pi * rand)
}
 
my @nums = randnorm(1, 0.5) xx 1000;
 
# Checking
say my $mean = @nums R/ [+] @nums;
say my $stddev = sqrt $mean**2 R- @nums R/ [+] @nums X** 2;
</lang>
 
=={{header|Phix}}==
Line 1,969 ⟶ 1,956:
(for/list ([i 1000])
(add1 (* (sqrt (* -2 (log (random)))) (cos (* 2 pi (random))) 0.5)))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|#22 "Thousand Oaks"}}
 
<lang perl6>sub randnorm ($mean, $stddev) {
$mean + $stddev * sqrt(-2 * log rand) * cos(2 * pi * rand)
}
 
my @nums = randnorm(1, 0.5) xx 1000;
 
# Checking
say my $mean = @nums R/ [+] @nums;
say my $stddev = sqrt $mean**2 R- @nums R/ [+] @nums X** 2;
</lang>
 
Line 2,056 ⟶ 2,058:
next i
</lang>
 
=={{header|Ruby}}==
<lang ruby>Array.new(1000) { 1 + Math.sqrt(-2 * Math.log(rand)) * Math.cos(2 * Math::PI * rand) }</lang>
10,333

edits