2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,13 +1,16 @@
|
|||
{{omit from|GUISS}}
|
||||
{{omit from|UNIX Shell|From the shell, we simply invoke the awk solution}}
|
||||
|
||||
The goal of this task is to generate a collection filled with
|
||||
1000 normally distributed random (or pseudorandom) numbers
|
||||
with a mean of 1.0 and a [[wp:Standard_deviation|standard deviation]] of 0.5
|
||||
;Task:
|
||||
Generate a collection filled with '''1000''' normally distributed random (or pseudo-random) numbers
|
||||
with a mean of '''1.0''' and a [[wp:Standard_deviation|standard deviation]] of '''0.5'''
|
||||
|
||||
|
||||
Many libraries only generate uniformly distributed random numbers.
|
||||
|
||||
If so, use [[wp:Normal_distribution#Generating_values_from_normal_distribution|this formula]] to convert them to a normal distribution.
|
||||
|
||||
;See also:
|
||||
* [[Standard deviation]]
|
||||
|
||||
;Related task:
|
||||
* [[Standard deviation]]
|
||||
<br><br>
|
||||
|
|
|
|||
16
Task/Random-numbers/Elixir/random-numbers-1.elixir
Normal file
16
Task/Random-numbers/Elixir/random-numbers-1.elixir
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
defmodule Random do
|
||||
def normal(mean, sd) do
|
||||
{a, b} = {:rand.uniform, :rand.uniform}
|
||||
mean + sd * (:math.sqrt(-2 * :math.log(a)) * :math.cos(2 * :math.pi * b))
|
||||
end
|
||||
end
|
||||
|
||||
std_dev = fn (list) ->
|
||||
mean = Enum.sum(list) / length(list)
|
||||
sd = Enum.reduce(list, 0, fn x,acc -> acc + (x-mean)*(x-mean) end) / length(list)
|
||||
|> :math.sqrt
|
||||
IO.puts "Mean: #{mean},\tStdDev: #{sd}"
|
||||
end
|
||||
|
||||
xs = for _ <- 1..1000, do: Random.normal(1.0, 0.5)
|
||||
std_dev.(xs)
|
||||
2
Task/Random-numbers/Elixir/random-numbers-2.elixir
Normal file
2
Task/Random-numbers/Elixir/random-numbers-2.elixir
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
xs = for _ <- 1..1000, do: 1.0 + :rand.normal * 0.5
|
||||
std_dev.(xs)
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
defmodule Random do
|
||||
def init() do
|
||||
:random.seed(:erlang.now())
|
||||
end
|
||||
def normal(mean, sd) do
|
||||
{a, b} = {:random.uniform(), :random.uniform()}
|
||||
mean + sd * (:math.sqrt(-2 * :math.log(a)) * :math.cos(2 * :math.pi * b))
|
||||
end
|
||||
end
|
||||
|
||||
Random.init()
|
||||
xs = for _ <- 1..1000, do: Random.normal(1.0, 0.5)
|
||||
2
Task/Random-numbers/Maple/random-numbers.maple
Normal file
2
Task/Random-numbers/Maple/random-numbers.maple
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
with(Statistics):
|
||||
Sample(Normal(1, 0.5), 1000);
|
||||
35
Task/Random-numbers/PowerShell/random-numbers.psh
Normal file
35
Task/Random-numbers/PowerShell/random-numbers.psh
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
function Get-RandomNormal
|
||||
{
|
||||
[CmdletBinding()]
|
||||
Param ( [double]$Mean, [double]$StandardDeviation )
|
||||
|
||||
$RandomNormal = $Mean + $StandardDeviation * [math]::Sqrt( -2 * [math]::Log( ( Get-Random -Minimum 0.0 -Maximum 1.0 ) ) ) * [math]::Cos( 2 * [math]::PI * ( Get-Random -Minimum 0.0 -Maximum 1.0 ) )
|
||||
|
||||
return $RandomNormal
|
||||
}
|
||||
|
||||
# Standard deviation function for testing
|
||||
function Get-StandardDeviation
|
||||
{
|
||||
[CmdletBinding()]
|
||||
param ( [double[]]$Numbers )
|
||||
|
||||
$Measure = $Numbers | Measure-Object -Average
|
||||
$PopulationDeviation = 0
|
||||
ForEach ($Number in $Numbers) { $PopulationDeviation += [math]::Pow( ( $Number - $Measure.Average ), 2 ) }
|
||||
$StandardDeviation = [math]::Sqrt( $PopulationDeviation / ( $Measure.Count - 1 ) )
|
||||
return $StandardDeviation
|
||||
}
|
||||
|
||||
# Test
|
||||
$RandomNormalNumbers = 1..1000 | ForEach { Get-RandomNormal -Mean 1 -StandardDeviation 0.5 }
|
||||
|
||||
$Measure = $RandomNormalNumbers | Measure-Object -Average
|
||||
|
||||
$Stats = [PSCustomObject]@{
|
||||
Count = $Measure.Count
|
||||
Average = $Measure.Average
|
||||
StandardDeviation = Get-StandardDeviation -Numbers $RandomNormalNumbers
|
||||
}
|
||||
|
||||
$Stats | Format-List
|
||||
|
|
@ -1,45 +1,45 @@
|
|||
/*REXX pgm gens 1,000 normally distributed #s: mean=1, standard deviation.=½.*/
|
||||
numeric digits 20 /*the default decimal digit precision=9*/
|
||||
parse arg n seed . /*allow specification of N and the seed*/
|
||||
if n=='' | n==',' then n=1000 /*N: is the size of the array. */
|
||||
if seed\=='' then call random ,,seed /*SEED: for repeatable random numbers. */
|
||||
newMean=1 /*the desired new mean|arithmetic avg. */
|
||||
sd=1/2 /*the desired new standard deviation. */
|
||||
do g=1 for n /*generate N uniform random #'s (0,1].*/
|
||||
#.g = random(1,1e5) / 1e5 /*REXX's RANDOM BIF generates integers.*/
|
||||
end /*g*/ /* [↑] rand integers ──► fractions. */
|
||||
say ' old mean=' mean()
|
||||
say 'old standard deviation=' stdDev()
|
||||
call pi; pi2=pi+pi /*define pi and also 2 * pi. */
|
||||
/*REXX pgm generates 1,000 normally distributed numbers: mean=1, standard deviation=½.*/
|
||||
numeric digits 20 /*the default decimal digit precision=9*/
|
||||
parse arg n seed . /*allow specification of N and the seed*/
|
||||
if n=='' | n=="," then n=1000 /*N: is the size of the array. */
|
||||
if datatype(seed,'W') then call random ,,seed /*SEED: for repeatable random numbers. */
|
||||
newMean=1 /*the desired new mean (arithmetic avg)*/
|
||||
sd=1/2 /*the desired new standard deviation. */
|
||||
do g=1 for n /*generate N uniform random #'s (0,1].*/
|
||||
#.g = random(1, 1e5) / 1e5 /*REXX's RANDOM BIF generates integers.*/
|
||||
end /*g*/ /* [↑] random integers ──► fractions. */
|
||||
say ' old mean=' mean()
|
||||
say 'old standard deviation=' stdDev()
|
||||
call pi; pi2=pi * 2 /*define pi and also 2 * pi. */
|
||||
say
|
||||
do j=1 to n-1 by 2; m=j+1 /*step through the iterations by two. */
|
||||
_=sd * sqrt(ln(#.j) * -2) /*calculate the used-twice expression.*/
|
||||
#.j=_ * cos(pi2*#.m) + newMean /*utilize the Box─Muller method. */
|
||||
#.m=_ * sin(pi2*#.m) + newMean /*random number must be: (0,1] */
|
||||
do j=1 to n-1 by 2; m=j+1 /*step through the iterations by two. */
|
||||
_=sd * sqrt(ln(#.j) * -2) /*calculate the used-twice expression.*/
|
||||
#.j=_ * cos(pi2 * #.m) + newMean /*utilize the Box─Muller method. */
|
||||
#.m=_ * sin(pi2 * #.m) + newMean /*random number must be: (0,1] */
|
||||
end /*j*/
|
||||
say ' new mean=' mean()
|
||||
say 'new standard deviation=' stdDev()
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────subroutines──────────────────────────────────────────────────────────────────────*/
|
||||
mean: _=0; do k=1 for n; _=_+#.k; end; return _/n
|
||||
stdDev: _avg=mean(); _=0; do k=1 for n; _=_+(#.k-_avg)**2; end; return sqrt(_/n)
|
||||
say ' new mean=' mean()
|
||||
say 'new standard deviation=' stdDev()
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*───────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
|
||||
mean: _=0; do k=1 for n; _=_ + #.k; end; return _/n
|
||||
stdDev: _avg=mean(); _=0; do k=1 for n; _=_ + (#.k - _avg)**2; end; return sqrt(_/n)
|
||||
e: e =2.7182818284590452353602874713526624977572470936999595749669676277240766303535; return e /*digs overkill*/
|
||||
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164062862; return pi /* " " */
|
||||
r2r: return arg(1) // (2*pi()) /*normalize ang*/
|
||||
r2r: return arg(1) // (pi() * 2) /*normalize ang*/
|
||||
sin: procedure; parse arg x;x=r2r(x);numeric fuzz min(5,digits()-3);if abs(x)=pi then return 0;return .sincos(x,x,1)
|
||||
.sincos:parse arg z,_,i; x=x*x; p=z; do k=2 by 2; _=-_*x/(k*(k+i)); z=z+_; if z=p then leave; p=z; end; return z
|
||||
ln: procedure; parse arg x,f; call e; ig= x>1.5; is=1-2*(ig\==1); ii=0; xx=x; return .ln_comp()
|
||||
.ln_comp: do while ig&xx>1.5|\ig&xx<.5;_=e;do k=-1;iz=xx*_**-is;if k>=0&(ig&iz<1|\ig&iz>.5) then leave;_=_*_;izz=iz;end
|
||||
/*───────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
|
||||
ln: procedure; parse arg x,f; call e; ig= x>1.5; is=1 - 2 * (ig\==1); ii=0; xx=x
|
||||
do while ig&xx>1.5|\ig&xx<.5;_=e;do k=-1;iz=xx*_**-is;if k>=0&(ig&iz<1|\ig&iz>.5) then leave;_=_*_;izz=iz;end
|
||||
xx=izz;ii=ii+is*2**k;end;x=x*e**-ii-1;z=0;_=-1;p=z;do k=1;_=-_*x;z=z+_/k;if z=p then leave;p=z;end; return z+ii
|
||||
|
||||
cos: procedure; parse arg x; x=r2r(x); a=abs(x); hpi=pi*.5
|
||||
numeric fuzz min(6,digits()-3); if a=pi() then return -1
|
||||
if a=hpi | a=hpi*3 then return 0; if a=pi()/3 then return .5
|
||||
if a=pi()*2/3 then return -.5; return .sinCos(1,1,-1)
|
||||
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); i=; m.=9
|
||||
numeric digits 9; numeric form; h=d+6; if x<0 then do; x=-x; i='i'; end
|
||||
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_%2
|
||||
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
|
||||
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
|
||||
numeric digits d; return (g/1)i /*make complex if X < 0.*/
|
||||
/*───────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
|
||||
cos: procedure; parse arg x; x=r2r(x); a=abs(x); hpi=pi * .5
|
||||
numeric fuzz min(6, digits() - 3); if a=pi then return -1
|
||||
if a=hpi | a=hpi*3 then return 0; if a=pi/3 then return .5
|
||||
if a=pi * 2/3 then return -.5; return .sinCos(1,1,-1)
|
||||
/*───────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); numeric digits; h=d+6
|
||||
numeric form; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g * .5'e'_ %2
|
||||
m.=9; do j=0 while h>9; m.j=h; h=h%2 + 1; end /*j*/
|
||||
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
|
||||
numeric digits d; return g/1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue