Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,57 @@
math.randomseed(os.time())
math.random() -- First number after seeding not random - throw one away
function randList (n) -- Build table of size n
local numbers = {}
for i = 1, n do
table.insert(numbers, math.random()) -- range correct by default
end
return numbers
end
function mean (t) -- Find mean average of values in table t
local sum = 0
for k, v in pairs(t) do
sum = sum + v
end
return sum / #t
end
function stdDev (t) -- Find population standard deviation of table t
local squares, avg = 0, mean(t)
for k, v in pairs(t) do
squares = squares + ((avg - v) ^ 2)
end
local variance = squares / #t
return math.sqrt(variance)
end
function showHistogram (t) -- Draw histogram of given table to stdout
local histBars, compVal = {}
for range = 0, 9 do
histBars[range] = 0
for k, v in pairs(t) do
compVal = tonumber(string.format("%0.1f", v - 0.05))
if compVal == range / 10 then
histBars[range] = histBars[range] + 1
end
end
end
for k, v in pairs(histBars) do
io.write("0." .. k .. " " .. string.rep('=', v / #t * 200))
print(" " .. v)
end
print()
end
function showStats (tabSize) -- Create and display statistics info
local numList = randList(tabSize)
print("Table of size " .. #numList)
print("Mean average: " .. mean(numList))
print("Standard dev: " .. stdDev(numList))
showHistogram(numList)
end
for power = 2, 5 do -- Start of main procedure
showStats(10 ^ power)
end

View file

@ -6,5 +6,5 @@ for 100, 1_000, 10_000 -> $N {
$mean**2 R- $N R/ [+] @data »**» 2;
printf "%.1f %s\n", .key, '=' x (500 * .value.elems / $N)
for sort *.key, @data.classify: (10 * *).Int / 10;
say;
&say;
}

View file

@ -1,38 +1,35 @@
/*REXX pgm gens some random #s, shows bin histogram, finds mean & stdDev*/
numeric digits 20 /*use twenty digits precision, */
showDigs=digits()%2 /* ··· but only show ten digits.*/
parse arg size seed . /*allow specification: size, seed*/
if size=='' | size==',' then size=100 /*if not specified, then use 100.*/
if datatype(seed,'W') then call random ,,seed /*allow seed for RAND.*/
#.=0 /*count of numbers in each bin. */
do j=1 for size /*generate some random numbers. */
@.j=random(0,99999)/100000 /*express as a fraction.*/
_=substr(@.j'00',3,1) /*determine which bin it's in, */
#._=#._+1 /* ··· and bump its count. */
/*REXX pgm gens some random numbers, shows bin histogram, finds mean & stdDev.*/
numeric digits 20 /*use twenty decimal digits precision, */
showDigs=digits()%2 /* ··· but only show ten decimal digits*/
parse arg size seed . /*allow specification: size, and seed.*/
if size=='' | size==',' then size=100 /*Not specified? Then use the default.*/
if datatype(seed,'W') then call random ,,seed /*allow a seed for RAND BIF.*/
#.=0 /*count of the numbers in each bin. */
do j=1 for size /*generate some random numbers. */
@.j=random(0,99999)/100000 /*express it as a fraction. */
_=substr(@.j'00',3,1) /*determine which bin the number is in,*/
#._=#._+1 /* ··· and bump its count. */
end /*j*/
do k=0 for 10 /*show a histogram of the bins. */
lr='0.'k ; if k==0 then lr='0 ' /*adjust for low range.*/
hr='0.'||(k+1); if k==9 then hr='1 ' /* " " high range.*/
range=lr"──►"hr' ' /*construct the range. */
barPC=right(strip(left(format(100*#.k/size,,2),5)),5) /*comp %*/
say range barPC copies('',format(barPC*1,,0)) /*histo.*/
do k=0 for 10 /*show a histogram of the bins. */
lr='0.'k ; if k==0 then lr='0 ' /*adjust for the low range.*/
hr='0.'||(k+1); if k==9 then hr='1 ' /* " " " high range.*/
range=lr"──►"hr' ' /*construct the range. */
barPC=right(strip(left(format(100*#.k/size,,2),5)),5) /*comp %.*/
say range barPC copies('',format(barPC*1,,0)) /*histo. */
end /*k*/
say
say 'sample size = ' size; say
avg=mean(size) ; say ' mean = ' format(avg,,showDigs)
stddev=stddev(size); say ' stddev = ' format(stddev,,showDigs)
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────MEAN subroutine─────────────────────*/
mean: parse arg N .; $=0; do m=1 for N; $=$+@.m; end /*m*/
return $/n
/*──────────────────────────────────STDDEV subroutine───────────────────*/
stddev: parse arg N .; $=0; do s=1 for N; $=$+(@.s-avg)**2; end /*s*/
return sqrt($/n)
/*──────────────────────────────────SQRT subroutine─────────────────────*/
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
avg=mean(size) ; say ' mean = ' format(avg,,showDigs)
std=stdDev(size); say ' stdDev = ' format(std,,showDigs)
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
mean: parse arg N; $=0; do m=1 for N; $=$+@.m; end; return $/n
stdDev: parse arg N; $=0; do s=1 for N; $=$+(@.s-avg)**2; end; return sqrt($/n)
/*────────────────────────────────────────────────────────────────────────────*/
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.*/