June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,32 @@
import extensions.
import extensions'math.
randomNormal = [ ^ mathControl cos(2 * pi_value * randomGenerator nextReal)
* mathControl sqrt(-2 * mathControl ln(randomGenerator nextReal)) ].
program =
[
array<real> a := real<>(1000).
real tAvg := 0.
0 till(a length) do(:x)
[
a[x] := randomNormal() / 2 + 1.
tAvg += a[x].
].
tAvg /= a length.
console printLine("Average: ", tAvg).
real s := 0.
0 till(a length) do(:x)
[
s += mathControl power(a[x] - tAvg, 2)
].
s := mathControl sqrt(s / 1000).
console printLine("Standard Deviation: ", s).
console readChar.
].

View file

@ -1 +1,2 @@
USING: random ;
1000 [ 1.0 0.5 normal-random-float ] replicate

View file

@ -1,17 +1,46 @@
package main
import (
"fmt"
"math"
"math/rand"
"strings"
"time"
)
const mean = 1.0
const stdv = .5
const n = 1000
func main() {
var list [1000]float64
var list [n]float64
rand.Seed(time.Now().UnixNano())
for i := range list {
list[i] = mean + stdv*rand.NormFloat64()
}
// show computed mean and stdv of list
var s, sq float64
for _, v := range list {
s += v
}
cm := s / n
for _, v := range list {
d := v - cm
sq += d * d
}
fmt.Printf("mean %.3f, stdv %.3f\n", cm, math.Sqrt(sq/(n-1)))
// show histogram by hdiv divisions per stdv over +/-hrange stdv
const hdiv = 3
const hrange = 2
var h [1 + 2*hrange*hdiv]int
for _, v := range list {
bin := hrange*hdiv + int(math.Floor((v-mean)/stdv*hdiv+.5))
if bin >= 0 && bin < len(h) {
h[bin]++
}
}
const hscale = 10
for _, c := range h {
fmt.Println(strings.Repeat("*", (c+hscale/2)/hscale))
}
}

View file

@ -0,0 +1 @@
replicateM 1000 $ normal 1 0.5

View file

@ -0,0 +1,9 @@
import Data.Random
import Control.Monad
thousandRandomNumbers :: RVar [Double]
thousandRandomNumbers = replicateM 1000 $ normal 1 0.5
main = do
x <- sample thousandRandomNumbers
print x

View file

@ -0,0 +1,5 @@
-- Returns a random float value in range 0..1
on randf ()
n = random(the maxinteger)-1
return n / float(the maxinteger-1)
end

View file

@ -0,0 +1,4 @@
normal = []
repeat with i = 1 to 1000
normal.add(1 + sqrt(-2 * log(randf())) * cos(2 * PI * randf()) / 2)
end repeat

View file

@ -0,0 +1,25 @@
val distrubution = {
def randomNormal = 1.0 + 0.5 * scala.util.Random.nextGaussian
def normalDistribution(a: Double): Stream[Double] = a #:: normalDistribution(randomNormal)
normalDistribution(randomNormal)
}
/*
* Let's test it
*/
def calcAvgAndStddev[T](ts: Iterable[T])(implicit num: Fractional[T]): (T, Double) = {
val mean: T =
num.div(ts.sum, num.fromInt(ts.size)) // Leaving with type of function T
// Root of mean diffs
val stdDev = sqrt(ts.map { x =>
val diff = num.toDouble(num.minus(x, mean))
diff * diff
}.sum / ts.size)
(mean, stdDev)
}
println(calcAvgAndStddev(distrubution.take(1000))) // e.g. (1.0061433267806525,0.5291834867560893)

View file

@ -0,0 +1,36 @@
val urandomlist = fn seed => fn n =>
let
val uniformdeviate = fn seed =>
let
val in31m = (Real.fromInt o Int32.toInt ) (getOpt (Int32.maxInt,0) );
val in31 = in31m +1.0;
val s1 = 41160.0;
val s2 = 950665216.0;
val v = Real.realFloor seed;
val val1 = v*s1;
val val2 = v*s2;
val next1 = Real.fromLargeInt (Real.toLargeInt IEEEReal.TO_NEGINF (val1/in31)) ;
val next2 = Real.rem(Real.realFloor(val2/in31) , in31m );
val valt = val1+val2 - (next1+next2)*in31m;
val nextt = Real.realFloor(valt/in31m);
val valt = valt - nextt*in31m;
in
(valt/in31m,valt)
end;
val store = ref (0.0,0.0);
val rec u = fn S => fn 0 => [] | n=> (store:=uniformdeviate S; (#1 (!store)):: (u (#2 (!store)) (n-1))) ;
in
u seed n
end;
local
open Math
in
val bmconv = fn urand => fn vrand => 1.0+0.5*(sqrt(~2.0*ln urand)*cos (2.0*pi*vrand) )
end;
val rec makeNormals = fn once => fn u::v::[] => [once u v] |
u::v::rm => (once u v )::(makeNormals once rm );
val anyrealseed=1009.0 ;
makeNormals bmconv (urandomlist anyrealseed 2000);

View file

@ -0,0 +1 @@
a = rnormal(1000,1,1,0.5)