Skip to content

Instantly share code, notes, and snippets.

@jfbu
Created May 6, 2018 13:41
Show Gist options
  • Save jfbu/64f89eddffb6fedb4077022ab58ecbeb to your computer and use it in GitHub Desktop.
Save jfbu/64f89eddffb6fedb4077022ab58ecbeb to your computer and use it in GitHub Desktop.
Examine random bits from \pdfuniformdeviate 2 in batches of 55s or 165s. See ".out.txt" files.
\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
% \foo holds the number of initial shifts to be done
\def\Dzero{0}%
\def\Done{0}%
\pdfsetrandomseed #1
% do \foo times a call to \pdfuniformdeviate 2
\cnta 0
\xintloop
\ifnum\cnta<\foo\space
\edef\x{\pdfuniformdeviate 2}%
\advance\cnta 1
\repeat
% now do #2 times:
% examine if more 1s than 0s in next #3 \pdfuniformdeviate 2
\cnta 0
\xintloop {%
\def\S{0}%
\cntb 0
\xintloop
\ifodd\pdfuniformdeviate2\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
\immediate\write\out{(shift=\foo, seed=#1) 0:\Dzero, 1:\Done}%
}
\newwrite\out
\immediate\openout\out=\jobname.out
% Try with all 55 possible starting point
% (the special case "54" makes theoretical analysis a bit easier,
% when batches have a length multiple of 55)
% We only study runs of 165 draws here.
\immediate\write\out{+++++++++++++++++++++++}
\immediate\write\out{10000 runs of 165 draws}
\immediate\write\out{+++++++++++++++++++++++}
\immediate\write\out{}
\cntc 1
\xintloop
\edef\foo{\the\cntc}%
\testwalk {149847074}{10000}{165}%
\testwalk {256844633}{10000}{165}%
\immediate\write\out{}%
\ifnum\cntc<55
\advance\cntc 1
\repeat
\immediate\write\out{}
\immediate\write\out{++++++++++++++++++++++}
\immediate\write\out{30000 runs of 55 draws}
\immediate\write\out{++++++++++++++++++++++}
\immediate\write\out{}
\cntc 1
\xintloop
\edef\foo{\the\cntc}%
\testwalk {149847074}{30000}{55}%
\testwalk {256844633}{30000}{55}%
\immediate\write\out{}%
\ifnum\cntc<55
\advance\cntc 1
\repeat
\immediate\closeout\out
\bye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment