Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,3 +1,13 @@
|
|||
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
|
||||
{{omit from|GUISS}}
|
||||
{{omit from|UNIX Shell|From the shell, we simply invoke the awk solution}}
|
||||
|
||||
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.
|
||||
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
|
||||
|
||||
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]]
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- Probability and statistics
|
||||
- Randomness
|
||||
note: Basic language learning
|
||||
|
|
|
|||
12
Task/Random-numbers/AWK/random-numbers-2.awk
Normal file
12
Task/Random-numbers/AWK/random-numbers-2.awk
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
function r() {
|
||||
return sqrt( -2*log( rand() ) ) * cos(6.2831853*rand() )
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
n=1000
|
||||
for(i=0;i<n;i++) {
|
||||
x = 1 + 0.5*r()
|
||||
s = s" "x
|
||||
}
|
||||
print s
|
||||
}
|
||||
|
|
@ -10,8 +10,7 @@ struct NormalRandom {
|
|||
}
|
||||
|
||||
double opCall() const /*nothrow*/ {
|
||||
immutable r1 = uniform(0.0, 1.0), // Not nothrow.
|
||||
r2 = uniform(0.0, 1.0);
|
||||
immutable r1 = uniform01, r2 = uniform01; // Not nothrow.
|
||||
return mean + stdDev * sqrt(-2 * r1.log) * cos(2 * PI * r2);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
12
Task/Random-numbers/Elixir/random-numbers.elixir
Normal file
12
Task/Random-numbers/Elixir/random-numbers.elixir
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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)
|
||||
|
|
@ -1,24 +1,15 @@
|
|||
create or replace
|
||||
PROCEDURE PROCEDURE1 AS
|
||||
TYPE numsColl is TABLE OF NUMBER;
|
||||
nums numsColl;
|
||||
|
||||
FUNCTION GenNums(n IN NUMBER) RETURN numsColl AS
|
||||
PI NUMBER := ACOS (-1);
|
||||
BEGIN
|
||||
nums := numsColl();
|
||||
nums.extend(n);
|
||||
|
||||
FOR i in 1 .. n LOOP
|
||||
nums(i) := 1 + .5 * (sqrt(-2 * log(dbms_random.value, 10)) * cos(2 * PI * dbms_random.value));
|
||||
END LOOP;
|
||||
|
||||
RETURN nums;
|
||||
END GenNums;
|
||||
DECLARE
|
||||
--The desired collection
|
||||
type t_coll is table of number index by binary_integer;
|
||||
l_coll t_coll;
|
||||
|
||||
c_max pls_integer := 1000;
|
||||
BEGIN
|
||||
nums := GenNums(10);
|
||||
FOR i in 1 .. 10 LOOP
|
||||
DBMS_OUTPUT.PUT_LINE(nums(i));
|
||||
END LOOP;
|
||||
END PROCEDURE1;
|
||||
FOR l_counter IN 1 .. c_max
|
||||
LOOP
|
||||
-- dbms_random.normal delivers normal distributed random numbers with a mean of 0 and a variance of 1
|
||||
-- We just adjust the values and get the desired result:
|
||||
l_coll(l_counter) := DBMS_RANDOM.normal * 0.5 + 1;
|
||||
DBMS_OUTPUT.put_line (l_coll(l_counter));
|
||||
END LOOP;
|
||||
END;
|
||||
|
|
|
|||
9
Task/Random-numbers/Pascal/random-numbers.pascal
Normal file
9
Task/Random-numbers/Pascal/random-numbers.pascal
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function rnorm (mean, sd: real): real;
|
||||
{Calculates Gaussian random numbers according to the Box-Müller approach}
|
||||
var
|
||||
u1, u2: real;
|
||||
begin
|
||||
u1 := random;
|
||||
u2 := random;
|
||||
rnorm := mean * abs(1 + sqrt(-2 * (ln(u1))) * cos(2 * pi * u2) * sd);
|
||||
end;
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
import random
|
||||
values = [random.gauss(1, .5) for i in range(1000)]
|
||||
# or [ random.normalvariate(1, 0.5) for i in range(1000)]
|
||||
>>> import random
|
||||
>>> values = [random.gauss(1, .5) for i in range(1000)]
|
||||
>>>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,9 @@
|
|||
>>> mean = sum(values)/1000
|
||||
>>> sdeviation = (sum((i - mean)**2 for i in values)/1000)**0.5
|
||||
>>> mean, sdeviation
|
||||
(1.0127861555468178, 0.5006682783828207)
|
||||
>>> def quick_check(numbers):
|
||||
count = len(numbers)
|
||||
mean = sum(numbers) / count
|
||||
sdeviation = (sum((i - mean)**2 for i in numbers) / count)**0.5
|
||||
return mean, sdeviation
|
||||
|
||||
>>> quick_check(values)
|
||||
(1.0140373306786599, 0.49943411329234066)
|
||||
>>>
|
||||
|
|
|
|||
4
Task/Random-numbers/Python/random-numbers-3.py
Normal file
4
Task/Random-numbers/Python/random-numbers-3.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
>>> values = [ random.normalvariate(1, 0.5) for i in range(1000)]
|
||||
>>> quick_check(values)
|
||||
(0.990099111944864, 0.5029847005836282)
|
||||
>>>
|
||||
|
|
@ -1,41 +1,38 @@
|
|||
/*REXX pgm gens 1,000 normally distributed #s: mean=1, standard dev.=½. */
|
||||
call pi /*call subroutine to define pi. */
|
||||
parse arg n seed . /*allow specification of N | seed*/
|
||||
if n=='' | n==',' then n=1000 /* N is the size of the array. */
|
||||
if seed\=='' then call random ,,seed /*use seed for repeatable RANDOM#*/
|
||||
mean=1 /*desired new mean (arith. avg.) */
|
||||
/*REXX pgm gens 1,000 normally distributed #s: mean=1, standard dev.=½.*/
|
||||
numeric digits 20 /*greater precision than 9 digits*/
|
||||
parse arg n seed . /*allow specification of N & seed*/
|
||||
if n=='' | n==',' then n=1000 /*N: is the size of the array.*/
|
||||
if seed\=='' then call random ,,seed /*SEED: for repeatable random #'s*/
|
||||
newMean=1 /*desired new mean|arithmetic avg*/
|
||||
sd=1/2 /*desired new standard deviation.*/
|
||||
do g=1 for n /*generate N uniform random nums.*/
|
||||
#.g=random(0,1e5)/1e5 /*REXX gens uniform rand integers*/
|
||||
end /*g*/
|
||||
|
||||
do g=1 for n /*gen N uniform random #'s (0,1].*/
|
||||
#.g = random(1,1e5) / 1e5 /*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. */
|
||||
say
|
||||
do j=1 to n-1 by 2
|
||||
m=j+1
|
||||
_=sd*sqrt(-2*ln(#.j))*cos(2*pi*#.m)+mean /*use Box-Muller method*/
|
||||
#.m=sd*sqrt(-2*ln(#.j))*sin(2*pi*#.m)+mean /*rand # must be 0──<E29480>1.*/
|
||||
#.j=_
|
||||
end /*j*/
|
||||
do j=1 to n-1 by 2; m=j+1 /*step through iterations by two.*/
|
||||
_=sd * sqrt(ln(#.j) * -2) /*calculate 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 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)
|
||||
e: e=2.7182818284590452353602874713526624977572470936999595749669676277240766303535; return e
|
||||
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164062862; return pi
|
||||
r2r: return arg(1)//(2*pi())
|
||||
sqrt: procedure;parse arg x; if x=0 then return 0; d=digits(); numeric digits 11; g=.sqrtGuess()
|
||||
do j=0 while p>9; m.j=p; p=p%2+1; end; do k=j+5 to 0 by -1; if m.k>11 then numeric digits m.k
|
||||
g=.5*(g+x/g); end; numeric digits d; return g/1
|
||||
.sqrtGuess: numeric form; m.=11; p=d+d%4+2
|
||||
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; return g*.5'E'_%2
|
||||
cos: procedure; arg x; x=r2r(x); a=abs(x); numeric fuzz min(9,digits()-9); if a=pi() then return -1
|
||||
if a=pi()/2|a=2*pi() then return 0;if a=pi()/3 then return .5;if a=2*pi()/3 then return -.5;return .sincos(1,1,-1)
|
||||
sin: procedure; 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()
|
||||
/*──────────────────────────────────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)
|
||||
e: e =2.7182818284590452353602874713526624977572470936999595749669676277240766303535; return e /*digs overkill*/
|
||||
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164062862; return pi /* " " */
|
||||
r2r: return arg(1) // (2*pi()) /*normalize ang*/
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); numeric digits 11; numeric form; m.=11; p=d+d%4+2
|
||||
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'E'_%2; do j=0 while p>9; m.j=p; p=p%2+1; end
|
||||
do k=j+5 to 0 by -1; if m.k>11 then numeric digits m.k; g=.5*(g+x/g); end; numeric digits d; return g/1
|
||||
cos: procedure; parse arg x; x=r2r(x); a=abs(x); numeric fuzz min(9,digits()-9); if a=pi then return -1; pi2=pi+pi
|
||||
if a=pi*.5|a=pi2 then return 0; if a=pi/3 then return .5; if a=pi2/3 then return -.5; return .sincos(1,1,-1)
|
||||
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
|
||||
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
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue