In [1]:
using PyPlot
Aufgabe 3a)
In [2]:
function rand_hist(m, n, f=rand)
result = zeros(m)
for i in 1:n
result[Int(ceil(m*f()))] += 1
end
return result
end
Out[2]:
In [3]:
function plot_hist(m, n, hist)
# Hässlich aber ¯\_(*-*)_/¯
ys = [0]
for x in hist
push!(ys,0) # senkrechte vor dem Balken
for i in 1:98
push!(ys, x) # 98 mal der Wert als Oberseite des Balkens
end
push!(ys, 0) # senkrechte zum abschluss eines Balkens
end
plot(1:(1/100):(m+1),ys)
axis([1,m,0,(n/m)*2]) # y Achse soll bei 0 Anfangen sonst sieht man die Gleichverteilung nicht
xlabel("Wertebereich")
ylabel("Anzahl der Treffer")
end
Out[3]:
In [4]:
M = 10
N = 1000
plot_hist(M, N, rand_hist(M, N))
Out[4]:
Aufgabe 3b)
In [5]:
M = 10
Ns = 100:10000
results = []
for n in Ns
hist = rand_hist(M, n)
delta = 0
for i in hist
delta += abs(i/n-1/M)
end
push!(results, delta)
end
plot(Ns, results)
Out[5]:
Aufgabe 3c)
In [6]:
function lcg_fgen(s, vec)
state = s
function new_rand()
state = mod(vec[1]*state+vec[2],vec[3])
return state/vec[3]
end
return new_rand
end
Out[6]:
In [7]:
seeds = [1, 23, 64523]
params = [[12345, 678, 124612], [94, 500, 16420], [24512, 167237, 100000000]]
for k in 1:3
f = lcg_fgen(seeds[k], params[k])
p = 0
while(f()*params[k][3] != seeds[k] && p <= params[k][3])
p += 1
end
if(p <= params[k][3])
println("Für U0 = ", seeds[k], ", a = ", params[k][1], ", c = ", params[k][2], " und m = ", params[k][3], " ist die Periode p = ", p)
else
println("Für U0 = ", seeds[k], ", a = ", params[k][1], ", c = ", params[k][2], " und m = ", params[k][3], " gibt es keine periodizität")
end
end
In [8]:
function imlazy(x)
println("Für U0 = ", seeds[x], ", a = ", params[x][1], ", c = ", params[x][2], " und m = ", params[x][3], " ist die Verteilung:")
plot_hist(M, N, rand_hist(M, N, lcg_fgen(seeds[x], params[x])))
end
Out[8]:
In [9]:
M = 20
N = 10000
imlazy(1)
Out[9]:
In [10]:
imlazy(2)
Out[10]:
In [11]:
imlazy(3)
Out[11]: