Zufallstest

Initialisierungen

>    restart;

Wir benötigen das Paket StringTools für die Funktion Ord

>    with(StringTools):

Warning, the assigned name Group now has a global binding

Außerdem statplots für die Histogramme

>    with(stats[statplots]):

Warning, these names have been redefined: boxplot, histogram, scatterplot, xscale, xshift, xyexchange, xzexchange, yscale, yshift, yzexchange, zscale, zshift

getnr : Eine Funktion, die das erste Zeichen in eine Zahl umwandelt. Bei Fehler Rückgabe -1

>    getnr := proc(char)
local c;
c:=Ord(char)-48;
if (c < 0 or c > 9) then
return -1;
else
return c;
fi:
end:

>   

Beispiel:

>    getnr("2"); getnr("35");

2

3

>    getnr("A");

-1

>   

AnzahlZiffern : gibt die Anzahl der Ziffern (Vor- und Nachkommastellen) einer Zahl oder in einem String aus

>    AnzahlZiffern := proc(A)
local text, i, c, n;
n:=0;
text:=convert(evalf(A), string);
for i from 1 while text[i] <> "" do   # wir gehen den ganze Text bis zum Ende ("") durch
    c := getnr(text[i]);
    if (c <> -1) then   # falls ein Zeichen
        n := n+1;
    fi:
od:
return n;
end:

>   

Zufallstest1 : Nimmt eine Zahl / String an und gibt Anzahl der Ziffern, Anzahl 0en, 1en usw. und eine Liste mit den Wahrscheinlichkeiten zurück

>    zufallstest1 := proc(A)
local c, cs, n, i, text;
n:=0;
for i from 0 to 9 do:
cs[i] := 0;
od:

text:=convert(evalf(A), string);
for i from 1 while text[i] <> "" do
c := getnr(text[i]);
if (c <> -1) then
cs[c] := cs[c]+1;
n := n+1;
fi:
od:
# Ausgabe
printf("Zahlen: %d\n", n);
for i from 0 to 9 do:
printf("Anzahl %den: %d", i, cs[i]);
cs[i] := cs[i]/n;
printf(" (%f%%)\n", cs[i]*100);
od:

return eval(cs):
end:

Beispiele:

>    Digits:=10000; # Nachkommastellen auf 10000 setzen

Digits := 10000

>    c :=zufallstest1(Pi);

Zahlen: 10000

Anzahl 0en: 968 (9.680000%)

Anzahl 1en: 1026 (10.260000%)

Anzahl 2en: 1021 (10.210000%)

Anzahl 3en: 975 (9.750000%)

Anzahl 4en: 1012 (10.120000%)

Anzahl 5en: 1046 (10.460000%)

Anzahl 6en: 1021 (10.210000%)

Anzahl 7en: 969 (9.690000%)

Anzahl 8en: 948 (9.480000%)

Anzahl 9en: 1014 (10.140000%)

c := TABLE([0 = 121/1250, 1 = 513/5000, 2 = 1021/10000, 3 = 39/400, 4 = 253/2500, 5 = 523/5000, 6 = 1021/10000, 7 = 969/10000, 8 = 237/2500, 9 = 507/5000])

Ausgeben:

>    data:=[seq(Weight(i..i+1, c[i]), i=0..9)]:
histogram(data);

[Maple Plot]

>   

Zufallstest2 : Nimmt eine Zahl / String an und gibt Anzahl der Ziffern aus sowie die Häufigkeit der einzelnen Typen: 2. Argument ist die Verschiebung, so kann man bei 0, 1, 2, 3 oder 4 anfangen.
5erGruppen, T1 = abcde, T2 = aabcd, T3 = aabbc, T4 = aaabc, T5 = aaabb, T6 = aaaab, T7 = aaaaa, jeweils nach Häufigkeit geordnet

>    zufallstest2 := proc(A, k)
local c, cs, n, i, j, text, nT;
n:=0;
cs:=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
nT:=[0, 0, 0, 0, 0, 0, 0];
j:=0;
text:=convert(evalf(A), string);
for i from k+1 while text[i] <> "" do # Text durchgehen
    c := getnr(text[i]);

    if (c <> -1) then
        cs[c+1] := cs[c+1]+1;
        n := n+1; j:=j+1;
        if j = 5 then
            cs:=sort(cs);    # wir haben 5 zusammen, sortieren & Typen bestimmen
            if cs[10] = 1 then
                nT[1] := nT[1]+1;
            elif cs[10] = 2 then
                if cs[9] = 1 then
                    nT[2]:= nT[2]+1;
                else
                    nT[3] := nT[3]+1;
                fi:
            elif cs[10] = 3 then
                if cs[9] = 1 then
                    nT[4] := nT[4]+1;
                else
                    nT[5] := nT[5]+1;
                fi:
            elif cs[10] = 4 then
                nT[6] := nT[6]+1;
            else
                nT[7] := nT[7]+1;
            fi:
            cs:=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
            j:=0;
        fi:
    fi:
od:
# Ausgabe
printf("Zahlen: %d. ", n);
n := trunc((n-k) / 5);
printf("5er-Blöcke: %d\n", n);
for i from 1 to 7 do:
printf("Anzahl Typ T%d: %d", i, nT[i]);
nT[i] := nT[i]/n;
printf(" (%f%%)\n", nT[i]*100);
od:

return eval(nT):
end:

>   

Beispiel:

>    zufallstest2(Pi, 0);

Zahlen: 10000. 5er-Blöcke: 2000

Anzahl Typ T1: 589 (29.450000%)

Anzahl Typ T2: 1040 (52.000000%)

Anzahl Typ T3: 212 (10.600000%)

Anzahl Typ T4: 134 (6.700000%)

Anzahl Typ T5: 20 (1.000000%)

Anzahl Typ T6: 5 (0.250000%)

Anzahl Typ T7: 0 (0.000000%)

[589/2000, 13/25, 53/500, 67/1000, 1/100, 1/400, 0]

Man kann auch Daten aus einer Datei einlesen

>    c:=zufallstest2(readdata("c:/test.txt", string), 0);

Zahlen: 3519. 5er-Blöcke: 703

Anzahl Typ T1: 212 (30.156472%)

Anzahl Typ T2: 367 (52.204836%)

Anzahl Typ T3: 67 (9.530583%)

Anzahl Typ T4: 50 (7.112376%)

Anzahl Typ T5: 4 (0.568990%)

Anzahl Typ T6: 3 (0.426743%)

Anzahl Typ T7: 0 (0.000000%)

c := [212/703, 367/703, 67/703, 50/703, 4/703, 3/703, 0]

>    data:=[seq(Weight(i..i+1, c[i]*100), i=1..5)]:
histogram(data);

[Maple Plot]

>   

andere Beispiele:

>    getnr("90809890876");

9

>    zufallstest1("90809890876");

Zahlen: 11

Anzahl 0en: 3 (27.272727%)

Anzahl 1en: 0 (0.000000%)

Anzahl 2en: 0 (0.000000%)

Anzahl 3en: 0 (0.000000%)

Anzahl 4en: 0 (0.000000%)

Anzahl 5en: 0 (0.000000%)

Anzahl 6en: 1 (9.090909%)

Anzahl 7en: 1 (9.090909%)

Anzahl 8en: 3 (27.272727%)

Anzahl 9en: 3 (27.272727%)

TABLE([0 = 3/11, 1 = 0, 2 = 0, 3 = 0, 4 = 0, 5 = 0, 6 = 1/11, 7 = 1/11, 8 = 3/11, 9 = 3/11])

>    zufallstest2(8976098739045873049587345.999992349745345, 0);

Zahlen: 40. 5er-Blöcke: 8

Anzahl Typ T1: 5 (62.500000%)

Anzahl Typ T2: 1 (12.500000%)

Anzahl Typ T3: 1 (12.500000%)

Anzahl Typ T4: 0 (0.000000%)

Anzahl Typ T5: 0 (0.000000%)

Anzahl Typ T6: 0 (0.000000%)

Anzahl Typ T7: 1 (12.500000%)

[5/8, 1/8, 1/8, 0, 0, 0, 1/8]

Eigene Folgen zusammenstellen:

Dies funktioniert nur begrenzt, da die 0en am Anfang natürlich immer abgeschnitten werden, deswegen möglichst nur Funktionen verwenden die nur eine Ziffer zurückgeben

>    rand10:=rand(0..9); # gibt zahlen zwischen 0 und 9 zurück

rand10 := proc () local t; global _seed; _seed := irem(a*_seed,p); t := _seed;  to concats do _seed := irem(a*_seed,p); t := s*t+_seed end do; irem(t,divisor)+offset end proc

>    c:=0: # c zurücksetzen
for i from 1 to 100 do:
### cat hängt die nächste Zahl dran, statt rand10() könnte hier eine eigene Funktion stehen:
c:=cat(c, rand10());
od:
c;

`03662930521959645056978974229351360335451230378397690315284015736209315668879983964195279950564146642`

>    zufallstest2(c, 0);

Zahlen: 101. 5er-Blöcke: 20

Anzahl Typ T1: 6 (30.000000%)

Anzahl Typ T2: 10 (50.000000%)

Anzahl Typ T3: 4 (20.000000%)

Anzahl Typ T4: 0 (0.000000%)

Anzahl Typ T5: 0 (0.000000%)

Anzahl Typ T6: 0 (0.000000%)

Anzahl Typ T7: 0 (0.000000%)

[3/10, 1/2, 1/5, 0, 0, 0, 0]

>   

>    c:=0:
for i from 1 to 10 do:
### statt rand() steht jetzt i, also die schleifenvariable
c:=cat(c, i);
od:
c;

`012345678910`

viele Zahlen

>    c:=0:
for i from 1 to 5000 do:
c:=cat(c, rand10());
od:
# c; # das nicht ausgeben...

>    zufallstest1(c, 0);

Zahlen: 5001

Anzahl 0en: 536 (10.717856%)

Anzahl 1en: 485 (9.698060%)

Anzahl 2en: 501 (10.017996%)

Anzahl 3en: 522 (10.437912%)

Anzahl 4en: 503 (10.057988%)

Anzahl 5en: 505 (10.097980%)

Anzahl 6en: 501 (10.017996%)

Anzahl 7en: 462 (9.238152%)

Anzahl 8en: 465 (9.298140%)

Anzahl 9en: 521 (10.417916%)

TABLE([0 = 536/5001, 1 = 485/5001, 2 = 167/1667, 3 = 174/1667, 4 = 503/5001, 5 = 505/5001, 6 = 167/1667, 7 = 154/1667, 8 = 155/1667, 9 = 521/5001])

>