Skip to content

Instantly share code, notes, and snippets.

@jfbu
Last active May 6, 2018 09:00
Show Gist options
  • Save jfbu/bd40379ca5c595c686ebf7a5897d80b7 to your computer and use it in GitHub Desktop.
Save jfbu/bd40379ca5c595c686ebf7a5897d80b7 to your computer and use it in GitHub Desktop.
TeX file confirming that Python code emulated correctly \pdfuniformdeviate
\newcount\cnta
\newcount\cntb
%\newcount\cntc
%\newcount\cntd
\newcount\maxrand
\maxrand "10000000
% \the\maxrand
\input xinttools.sty
\def\testwalk#1#2#3{%
% #1 = seed (O or 1)
% #2 = number of repetitions
% #3 = length of batches; must be an odd integer
\pdfsetrandomseed #1
\cnta 0
\xintloop
\edef\x{\pdfuniformdeviate \maxrand}%
\advance\cnta 1
\ifnum\cnta<54
\repeat
\cnta 0
\def\Dzero{0}%
\def\Done{0}%
\xintloop {%
\def\S{0}%
\cntb 0
\xintloop
\ifodd\pdfuniformdeviate\maxrand\relax
\edef\S{\the\numexpr\S+1}%
\else
\edef\S{\the\numexpr\S-1}%
\fi
\advance\cntb 1
\ifnum\cntb < #3
\repeat
}\ifnum\S>0
\edef\Done{\the\numexpr\Done+1}%
\else
\edef\Dzero{\the\numexpr\Dzero+1}%
\fi
\advance\cnta 1
\ifnum\cnta < #2
\repeat
\noindent More 0s: \Dzero
\noindent More 1s: \Done
Observed probability of more 1s after #2 batches of #3 is
\the\dimexpr \numexpr\Done*65536/#2 sp\relax
}
\testwalk {0}{10000}{165}
\testwalk {1}{10000}{165}
\testwalk {0}{30000}{55}
\testwalk {1}{30000}{55}
\bye
PRODUCES:
Avec 10000 x 165 et 30000 x 55:
More 0s: 4655
More 1s: 5345
Observed probability of more 1s after 10000 batches of 165 is 0.5345pt
More 0s: 4603
More 1s: 5397
Observed probability of more 1s after 10000 batches of 165 is 0.5397pt
More 0s: 14932
More 1s: 15068
Observed probability of more 1s after 30000 batches of 55 is 0.50227pt
More 0s: 15079
More 1s: 14921
Observed probability of more 1s after 30000 batches of 55 is 0.49736pt
Avec 100000 x 165 et 300000 x 55:
More 0s: 46630
More 1s: 53370
Observed probability of more 1s after 100000 batches of 165 is 0.5337pt
More 0s: 46906
More 1s: 53094
Observed probability of more 1s after 100000 batches of 165 is 0.53094pt
More 0s: 149868
More 1s: 150132
Observed probability of more 1s after 300000 batches of 55 is 0.50044pt
More 0s: 150226
More 1s: 149774
Observed probability of more 1s after 300000 batches of 55 is 0.49925pt
et avec 1000000 fois 165 vs 3000000 fois 55:
More 0s: 468404
More 1s: 531596
Observed probability of more 1s after 1000000 batches of 165 is 0.5316pt
More 0s: 467180
More 1s: 532820
Observed probability of more 1s after 1000000 batches of 165 is 0.53282pt
More 0s: 1500787
More 1s: 1499213
Observed probability of more 1s after 3000000 batches of 55 is 0.49974pt
More 0s: 1498957
More 1s: 1501043
Observed probability of more 1s after 3000000 batches of 55 is 0.50035pt
sur l'iMac (2.8Ghz):
real 19m18.345s
user 19m18.240s
sys 0m0.203s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment