<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
  
  <title>Charles Zhu&#39;s blog</title>
  <subtitle>some thoughts</subtitle>
  <link href="https://synkathairo.github.io/blog/feed.xml" rel="self" />
  <link href="https://synkathairo.github.io/blog/" />
  <updated>2022-07-26T00:00:00Z</updated>
  <id>https://synkathairo.github.io/blog/</id>
  <author>
    <name>Charles Zhu</name>
  </author>
  <entry>
    <title>A journey into basic hardware design with an Arduino Uno R3</title>
    <link href="https://synkathairo.github.io/posts/2021-12-21-arduinouno/" />
    <updated>2021-12-21T00:00:00Z</updated>
    <id>https://synkathairo.github.io/posts/2021-12-21-arduinouno/</id>
    <content type="html">&lt;p&gt;I recently decided to obtain an &lt;a href=&quot;https://store-usa.arduino.cc/products/arduino-uno-rev3/&quot;&gt;Arduino
Uno R3&lt;/a&gt;, which, if we are to trust its online reputation, is supposed
to be a promising platform for prototyping hardware development. The
device came in a starter kit box with some other provided components
such as resistors, a breadboard, a USB cable, LCD display, wires, LEDs,
a tri-state 8-bit shift register, and a variety of sensors. It is fair
to say that the kit promised a decent variety of components for a
beginner to dive into Arduino &lt;a href=&quot;https://www.arduino.cc/en/uploads/Main/Arduino_Uno_Rev3-schematic.pdf&quot;&gt;hardware&lt;/a&gt;
experimentation.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.sparkfun.com//assets/parts/6/3/4/3/11021-01.jpg&quot; alt=&quot;Arduino Uno R3&quot; width=&quot;150&quot;&gt;&lt;/p&gt;
&lt;p&gt;Having obtained the hardware, the next step was to set up a
development environment. The &lt;a href=&quot;https://www.arduino.cc/en/software&quot;&gt;Arduino website&lt;/a&gt; provided
two options for my computer: I could use the Arduino IDE, or use the
Arduino command-line utility (&lt;code&gt;arduino-cli&lt;/code&gt;). In theory,
everything that could be done in the official IDE could be done using
the command-line utility. I counterintuitively decided to try using the
command-line utility first. I was able to download it for macOS using
Homebrew (&lt;code&gt;brew install arduino-cli&lt;/code&gt;). Then, I decided to use
VS Code to install the official &lt;a href=&quot;https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.vscode-arduino&quot;&gt;Arduino
extension&lt;/a&gt; made by Microsoft.&lt;/p&gt;
&lt;p&gt;The extension did not work with &lt;code&gt;arduino-cli&lt;/code&gt; out the box.
It did not immediately detect the plugged in device (although it was
detected by running &lt;code&gt;arduino-cli board list&lt;/code&gt; in the shell).
Several settings had to be set to allow the device to work:
&lt;code&gt;&quot;arduino.commandPath&quot;: &quot;arduino-cli&quot;, &quot;arduino.enableUSBDetection&quot;: true, &quot;arduino.useArduinoCli&quot;: true, &quot;arduino.path&quot;: &quot;/opt/homebrew/bin/&quot;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To make a project, the next step was to run
&lt;code&gt;arduino-cli new BareMinimum&lt;/code&gt; in the shell, where
&lt;code&gt;BareMinimum&lt;/code&gt; was my project title. This also works if you
manually make a folder and then make a file called
&lt;code&gt;BareMinimum.ino&lt;/code&gt; within it. Within it you can put at a
minimum the &lt;a href=&quot;https://www.arduino.cc/en/Tutorial/BuiltInExamples/BareMinimum&quot;&gt;following
code&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// put your setup code here, to run once:&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// put your main code here, to run repeatedly:&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After saving the code the next step was to ensure the interface with
the actual Uno device. First I could run
&lt;code&gt;arduino-cli board list&lt;/code&gt; to identify the FQBN (fully
qualified board name) for my board. The next step was to search for
available ‘cores’ that would work with the board. The next step was to
run &lt;code&gt;arduino-cli board listall uno&lt;/code&gt;. Following that I ran
&lt;code&gt;arduino-cli core install arduino:avr&lt;/code&gt;. I could now compile
by running
&lt;code&gt;arduino-cli compile --fqbn arduino:avr:uno BareMinimum&lt;/code&gt; and
then I could upload with
&lt;code&gt;arduino-cli upload -p /dev/location --fqbn arduino:avr:uno BareMinimum&lt;/code&gt;
where &lt;code&gt;/dev/location/&lt;/code&gt; would be subsituted by the appropriate
&lt;code&gt;/dev&lt;/code&gt; entry copied from the output of
&lt;code&gt;arduino-cli board list&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After doing all this… the code was uploaded! Nothing of significance
happened other than the light from the default LED remaining on.
However, by creating &lt;a href=&quot;https://create.arduino.cc/projecthub/B45i/getting-started-with-arduino-cli-7652a5&quot;&gt;another
project&lt;/a&gt; it would be possible to observe a blinking effect.&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;LED_BUILTIN&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; OUTPUT&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;LED_BUILTIN&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; HIGH&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;LED_BUILTIN&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; LOW&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Later I was also able to use the VS Code extension and its &lt;a href=&quot;https://maker.pro/arduino/tutorial/how-to-use-visual-studio-code-for-arduino&quot;&gt;board
configuration settings&lt;/a&gt; to upload code from the IDE directly.&lt;/p&gt;
&lt;p&gt;As you might notice from the example code, the Arduino language is a
customized dialect of C++.&lt;/p&gt;
&lt;p&gt;Following &lt;a href=&quot;https://www.circuitbasics.com/arduino-7-segment-display-tutorial/&quot;&gt;another
guide&lt;/a&gt; it was possible to proceed to the next step. The next step was
to try to use this board with some other hardware! I was able to use a
basic LED:&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; OUTPUT&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; LOW&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; 
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This was wired as follows (refer to the &lt;a href=&quot;https://www.circuitbasics.com/arduino-7-segment-display-tutorial/&quot;&gt;original
link&lt;/a&gt; for more details), using a 1K Ω resistor:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://www.circuitbasics.com/wp-content/uploads/2017/05/Arduino-7-Segment-Display-Tutorial-Cathode-to-GPIO.png&quot; alt=&quot;single-LED&quot; width=&quot;250&quot;&gt;&lt;/p&gt;
&lt;p&gt;I also found it helpful to check resistor values using an &lt;a href=&quot;https://www.digikey.com/en/resources/conversion-calculators/conversion-calculator-resistor-color-code&quot;&gt;online
resistor calculator&lt;/a&gt;. Resistance values in ohms are typically
indicated by colored bands which correspond to certain values. It is
important to note which side you begin reading the bands from, since you
could obtain different values. An &lt;a href=&quot;https://www.arrow.com/en/research-and-events/articles/resistor-color-code&quot;&gt;online
source&lt;/a&gt; suggested reading from the side with grouped bands (never
from the side with a metallic band). The resistor here plays the role of
limiting current (as we know from the elementary formulation of Ohm’s
Law, &lt;em&gt;I = V/R&lt;/em&gt;) and therefore a higher resistance could be used
to limit further the intensity/brightness of the LED. It is important to
use resistors in electronics to protect against excessive current from
damaging more sensitive components.&lt;/p&gt;
&lt;p&gt;Another interesting feature of this simple circuit is to note that
the LEDs used (any LEDs in fact) exhibit a characteristic &lt;a href=&quot;https://www.switchelectronics.co.uk/blog/post/ledpolarity.html&quot;&gt;polarity&lt;/a&gt;,
that is, current can only flow in one direction and thus the LED must be
wired accordingly. LEDs, or light-emitting diodes, like other &lt;a href=&quot;http://hyperphysics.phy-astr.gsu.edu/hbase/Solids/diod.html&quot;&gt;diodes&lt;/a&gt;
operate due to a &lt;a href=&quot;https://en.wikipedia.org/wiki/P%E2%80%93n_junction&quot;&gt;p-n
junction&lt;/a&gt; which is possible due to the use of semiconductors and
impurities which ‘bias’ the current to only flow, for the most part, in
one direction. As can be noted on the above image, the 5 volts are on
the side of the the anode (+) side of the LED, also wired in series with
the resistor. The cathode (-) side is connected to pin 7, which has a
‘LOW OUTPUT’ set by our code, which in according to the &lt;a href=&quot;https://www.arduino.cc/reference/en/language/variables/constants/constants/&quot;&gt;code
documentation&lt;/a&gt; actually sets the pin to 0 volts. In theory, our setup
would be similar to &lt;a href=&quot;https://learn.sparkfun.com/tutorials/light-emitting-diodes-leds/all&quot;&gt;doing
this&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.sparkfun.com/assets/6/e/8/3/c/51f93d85757b7f2049270817.png&quot; alt=&quot;circuit_cartoon&quot; width=&quot;250&quot;&gt;&lt;/p&gt;
&lt;p&gt;Or, if we were to follow the standard circuitry schematic, it would
be &lt;a href=&quot;https://circuitdigest.com/electronic-circuits/simplle-led-circuit-diagram&quot;&gt;similar
to this&lt;/a&gt;
&lt;!--(http://www.electronicshobbyprojects.com/basic-circuits/simplest-led-circuit.html)--&gt;
(albeit with different numerical values for voltage and resistance
etc):&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://circuitdigest.com/sites/default/files/circuitdiagram/simple-led-ckt_0.png&quot; alt=&quot;LED_circuitDiagram&quot; width=&quot;250&quot;&gt;&lt;/p&gt;
&lt;p&gt;Nominally/conventionally, ‘current’ flows from positive to negative,
that is, from our anode side to our cathode side (in actuality it is
more like the electrons ‘flow’ from the negative to the positive side).
Our current works out to be a little less than 5V/1000Ω = 5mA (unknown
resistance of the LED itself).&lt;/p&gt;
&lt;p&gt;It is also important to note the way that breadboards are wired.
Supposedly, breadboards are named as such due to the &lt;a href=&quot;https://en.wikipedia.org/wiki/Breadboard#Evolution&quot;&gt;historic usage
of actual bread cutting boards for prototyping circuits&lt;/a&gt;. The wiring
pattern of breadboards follows &lt;a href=&quot;https://www.sciencebuddies.org/science-fair-projects/references/how-to-use-a-breadboard&quot;&gt;this
pattern&lt;/a&gt;, where each slot within a rows is connected to the others in
the row (not going through the middle however), as are each slot in a
side column:
&lt;!-- (http://wiring.org.co/learning/tutorials/breadboard/): --&gt;&lt;/p&gt;
&lt;!-- ![breadboard](http://wiring.org.co/learning/tutorials/breadboard/imgs/breadboard-02.jpg){: width=&quot;400&quot;} --&gt;
&lt;p&gt;&lt;img src=&quot;https://www.sciencebuddies.org/Files/7326/6/breadboard-row-connections.png&quot; alt=&quot;breadboardwiring&quot; width=&quot;300&quot;&gt;&lt;/p&gt;
&lt;p&gt;This was just a very elementary demonstration of how circuitry could
be incorporated with hardware and software, and our humble &lt;a href=&quot;https://www.microchip.com/en-us/product/ATmega328P&quot;&gt;ATMega328P
microcontroller&lt;/a&gt; is sufficient to work with this circuitry!&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Note: the code snippets in this page come from the linked pages,
please refer to linked pages for source information and images as well
as further details.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Random number generators, deterministic systems and the difficulty of modeling stochasticity</title>
    <link href="https://synkathairo.github.io/posts/2022-04-13-rng-thoughts/" />
    <updated>2022-04-13T00:00:00Z</updated>
    <id>https://synkathairo.github.io/posts/2022-04-13-rng-thoughts/</id>
    <content type="html">&lt;p&gt;When using the R language I decided to look up the documentation of
the &lt;a href=&quot;https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/Random&quot;&gt;&lt;code&gt;Random&lt;/code&gt;&lt;/a&gt;
function which actually seems to be quite interesting. This
&lt;code&gt;Random&lt;/code&gt; function in the R &lt;code&gt;base&lt;/code&gt; library offers
several different pseudorandom number generators: the &lt;a href=&quot;https://en.wikipedia.org/wiki/Wichmann%E2%80%93Hill&quot;&gt;Wichmann-Hill&lt;/a&gt;,
&lt;a href=&quot;https://en.wikipedia.org/wiki/Multiply-with-carry_pseudorandom_number_generator&quot;&gt;Marsaglia-Multicarry&lt;/a&gt;,
Super Duper, &lt;a href=&quot;https://en.wikipedia.org/wiki/Mersenne_Twister&quot;&gt;Mersenne-Twister&lt;/a&gt;,
&lt;a href=&quot;https://www-cs-faculty.stanford.edu/~knuth/news02.html&quot;&gt;Knuth-TAOCP-2002&lt;/a&gt;,
Knuth-TAOCP, and &lt;a href=&quot;https://pubsonline.informs.org/doi/abs/10.1287/opre.47.1.159&quot;&gt;L’Ecuyer-CMRG&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://coolbutuseless.github.io/2020/07/07/the-quality-of-rs-random-number-generators/&quot;&gt;Another
blog&lt;/a&gt; I found has summarized the apparent performance advantages and
disadvantages of each of these random number generators (RNGs) so I will
link it here. The data in the post is taken from &lt;a href=&quot;https://www.iro.umontreal.ca/~lecuyer/myftp/papers/testu01.pdf&quot;&gt;L’Ecuyer
and Simard, 2007&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Firstly some overall philosophical notes about the nature of
computers as deterministic state machines. It is interesting that such
machines require algorithms such as these to even attempt to simulate
stochastic behavior! One thinks of human beings as the opposite of
computers: it is easier for us to be “stochastic” and it is hard for us
to be reliably “deterministic” and implement (for most people) complex
algorithms mentally in an efficient manner. In contrast, a computer in
the macro sense can only have a certain amount of states, such that we
can consider each possible arrangement of bytes at a given moment in CPU
registers, in main memory, in storage etc, to be a finite (though large)
number of possible states. That is, (classical) computers suffer a
discreteness limitation, as their fundamental operation requires this in
accordance with their theoretical model of binary logic, as well as the
practical implementation of this.&lt;/p&gt;
&lt;p&gt;One notes that (pseudo) RNGs tend to utilize certain “seeds” or
certain input values to be fed into RNG algorithms to produce a given
pseudorandom number. This tends to be something such as the timestamp
(which can reliably be said to different between every usage) although
this does not prevent certain problems. Some &lt;a href=&quot;https://scicomp.stackexchange.com/questions/30479/how-good-are-current-implementations-of-rand-in-c&quot;&gt;criticisms&lt;/a&gt;
have been noted for example of the default &lt;code&gt;rand()&lt;/code&gt; function
in many C implementations such as that in &lt;a href=&quot;https://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/rand.c;h=9c90e77af5d797894a1c50f1bb84c136dc5deb80;hb=HEAD&quot;&gt;glibc&lt;/a&gt;.
The implementation in &lt;a href=&quot;https://git.musl-libc.org/cgit/musl/tree/src/prng/rand.c&quot;&gt;musl&lt;/a&gt;
is quite simple:&lt;/p&gt;
&lt;pre class=&quot;language-c&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;stdlib.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;stdint.h&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;uint64_t&lt;/span&gt; seed&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;srand&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;unsigned&lt;/span&gt; s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    seed &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; s&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    seed &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6364136223846793005ULL&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;seed &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; seed&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The method utlized by musl appears to be a &lt;a href=&quot;https://en.wikipedia.org/wiki/Linear_congruential_generator#:~:text=264-,6364136223846793005,-1&quot;&gt;linear
congruential generator&lt;/a&gt;, whereas the reference implementation is by
Knuth. It is noted that LCGs are &lt;a href=&quot;https://crypto.stackexchange.com/questions/60543/how-are-linear-congruential-generator-multipliers-chosen&quot;&gt;not
sufficiently secure for “cryptographic applications”&lt;/a&gt;. The algorithm
follows the &lt;a href=&quot;https://www.eg.bucknell.edu/~xmeng/Course/CS6337/Note/master/node40.html&quot;&gt;formula&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;&lt;math display=&quot;block&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;X&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;true&quot; form=&quot;prefix&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;X&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mo stretchy=&quot;true&quot; form=&quot;postfix&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mspace width=&quot;0.444em&quot;&gt;&lt;/mspace&gt;&lt;mo&gt;mod&lt;/mo&gt;&lt;mspace width=&quot;0.222em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;/mrow&gt;&lt;mo&gt;,&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo&gt;,&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;,&lt;/mo&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mo&gt;,&lt;/mo&gt;&lt;mi&gt;.&lt;/mi&gt;&lt;mi&gt;.&lt;/mi&gt;&lt;mi&gt;.&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;X_{i+1} = (aX_i + c) &#92;mod m, i=0,1,2,...&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/p&gt;
&lt;p&gt;Here &lt;em&gt;a&lt;/em&gt; is the constant multiplier, &lt;em&gt;c&lt;/em&gt; is the
increment, &lt;em&gt;m&lt;/em&gt; is the modulus, hence in the above musl
implementation, the multiplier is 6364136223846793005, the increment is
1, and the modulus is 2&lt;sup&gt;64&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;The glibc &lt;code&gt;random()&lt;/code&gt; code is interesting because it
provides &lt;a href=&quot;https://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/random.c#l59&quot;&gt;very
extensive comments&lt;/a&gt;. The code comments note:
&lt;/p&gt;&lt;blockquote&gt;In addition to the
standard &lt;code&gt;rand()&lt;/code&gt;/&lt;code&gt;srand()&lt;/code&gt; like interface, this
package also has a special state info interface. The
&lt;code&gt;initstate()&lt;/code&gt; routine is called with a seed, an array of
bytes, and a count of how many bytes are being passed in; this array is
then initialized to contain information for random number generation
with that much state information. Good sizes for the amount of state
information are 32, 64, 128, and 256 bytes. The state can be switched by
calling the &lt;code&gt;setstate()&lt;/code&gt; function with the same array as was
initialized with &lt;code&gt;initstate()&lt;/code&gt;. By default, the package runs
with 128 bytes of state information and generates far better random
numbers than a linear congruential generator. If the amount of state
information is less than 32 bytes, a simple linear congruential R.N.G.
is used.&lt;/blockquote&gt;&lt;p&gt;&lt;/p&gt;
&lt;p&gt;What method is utilized instead of a linear congruential RNG?
&lt;/p&gt;&lt;blockquote&gt;The random number generation technique is a linear feedback shift
register approach, employing trinomials (since there are fewer terms to
sum up that way).&lt;/blockquote&gt;&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The man page states: 
&lt;/p&gt;&lt;blockquote&gt;The &lt;code&gt;random()&lt;/code&gt; function uses a
nonlinear additive feedback random number generator employing a default
table of size 31 long integers to return successive pseudo-random
numbers in the range from 0 to &lt;code&gt;RAND_MAX&lt;/code&gt;. The period of this
random number generator is very large, approximately 16 * ((2^31) -
1).&lt;/blockquote&gt;&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/random.c#l146&quot;&gt;In
the code&lt;/a&gt; we observe the table:&lt;/p&gt;
&lt;pre class=&quot;language-c&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;int32_t&lt;/span&gt; randtbl&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;DEG_3 &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    TYPE_3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1726662223&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;379960547&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1735697613&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1040273694&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1313901226&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token number&quot;&gt;1627687941&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;179304937&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2073333483&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1780058412&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1989503057&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;615974602&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;344556628&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;939512070&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1249116260&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1507946756&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;812545463&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;154635395&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1388815473&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1926676823&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;525320961&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1009028674&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;968117788&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;123449607&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1284210865&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;435012392&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2017506339&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;911064859&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;370259173&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1132637927&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1398500161&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;205601318&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which such trinomials are utilized? &lt;a href=&quot;https://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/random.c#l94&quot;&gt;The
code&lt;/a&gt; details 4 types of RNGs, with &lt;code&gt;TYPE_0&lt;/code&gt; being the
simple linear congruential method, the others being trinomials:
&lt;code&gt;TYPE_1&lt;/code&gt;
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;7&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;x^7 + x^3 + 1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;,
&lt;code&gt;TYPE_2&lt;/code&gt;
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;15&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;x^15 + x + 1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;,
and &lt;code&gt;TYPE_3&lt;/code&gt;
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;31&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;x^31 + x^3 + 1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;.
These three trinomial equations intersect at the points (0,1), (1,3) and
(-1,-1), while additional intersections exist between the pairs at
non-integer points:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://synkathairo.github.io/assets/2022/04/desmos-graph.png&quot; alt=&quot;plot_trinomials&quot; width=&quot;50%&quot;&gt;&lt;/p&gt;
&lt;p&gt;These trinomials are important enough that &lt;a href=&quot;https://www.unf.edu/~cwinton/html/cop4300/s09/class.notes/c1-VLP-RNGs.pdf&quot;&gt;lists
have been published of them&lt;/a&gt; and they have been subject of study.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.redhat.com/en/blog/understanding-random-number-generators-and-their-limitations-linux&quot;&gt;method
utilized by glibc&lt;/a&gt; in &lt;code&gt;random_r()&lt;/code&gt; involves the &lt;a href=&quot;https://github.com/lattera/glibc/blob/master/stdlib/random_r.c#L364&quot;&gt;formula&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-c&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;int32_t&lt;/span&gt; val &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1103515245U&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;12345U&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x7fffffff&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the GNU man page is detailed the differences between
&lt;code&gt;random()&lt;/code&gt; and &lt;code&gt;random_r()&lt;/code&gt;:
&lt;/p&gt;&lt;blockquote&gt;The &lt;code&gt;random_r()&lt;/code&gt; function is like &lt;code&gt;random(3)&lt;/code&gt;, except
that instead of using state information maintained in a global variable,
it uses the state information in the argument pointed to by buf, which
must have been previously initialized by &lt;code&gt;initstate_r()&lt;/code&gt;. The
generated random number is returned in the argument result.&lt;/blockquote&gt;&lt;p&gt;&lt;/p&gt;
&lt;p&gt;A &lt;a href=&quot;https://en.wikipedia.org/wiki/Linear-feedback_shift_register&quot;&gt;linear
feedback shift register&lt;/a&gt; utilizes “a &lt;a href=&quot;https://en.wikipedia.org/wiki/Linearity#Boolean_functions&quot;&gt;linear
function&lt;/a&gt; of its previous state” as the input bit.&lt;/p&gt;
&lt;p&gt;Another set of random number generators in UNIX and UNIX-like systems
(such as macOS, BSD, Linux) occurs in the exposed
&lt;code&gt;/dev/random&lt;/code&gt; and &lt;code&gt;/dev/urandom&lt;/code&gt; “files”. Under
the UNIX paradigm “everything is a file” so these “files” can be handled
by utilities such as &lt;code&gt;cat&lt;/code&gt; as if they were files, allowing
pipelining to files etc. These produce continuous streams of random
bytes which are considered &lt;a href=&quot;https://en.wikipedia.org/wiki//dev/random#Linux&quot;&gt;“cryptographically
secure”&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Let’s examine the randomness tests contained within the TestU01 suite
proposed by &lt;a href=&quot;https://www.iro.umontreal.ca/~lecuyer/myftp/papers/testu01.pdf&quot;&gt;L’Ecuyer
and Simard, 2007&lt;/a&gt;. They first define a “good imitation” of
“independent uniform random variables” (colloquially, what we would
presumably consider “truly random”): &lt;/p&gt;&lt;blockquote&gt;RNGs for all types of
applications are designed so that their output sequence is a good
imitation of a sequence of independent uniform random variables, usually
over the real interval
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;true&quot; form=&quot;prefix&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo&gt;,&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo stretchy=&quot;true&quot; form=&quot;postfix&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;(0, 1)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;
or over the binary set
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot; form=&quot;prefix&quot;&gt;{&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo&gt;,&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo stretchy=&quot;false&quot; form=&quot;postfix&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;&#92;{0, 1&#92;}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;.
In the first case, the relevant hypothesis
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/msubsup&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;H_0^A&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;
to be tested is that the successive output values of the RNG, say
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;,&lt;/mo&gt;&lt;mi&gt;.&lt;/mi&gt;&lt;mi&gt;.&lt;/mi&gt;&lt;mi&gt;.&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;u_0 , u_1 , u_2 , . . .&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;,
are independent random variables from the uniform distribution over the
interval
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;true&quot; form=&quot;prefix&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo&gt;,&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo stretchy=&quot;true&quot; form=&quot;postfix&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;(0, 1)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;,
that is, i.i.d.
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;true&quot; form=&quot;prefix&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo&gt;,&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo stretchy=&quot;true&quot; form=&quot;postfix&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;U (0, 1)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;.
In the second case,
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/msubsup&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;H_0^B&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;
says that we have a sequence of independent random bits, each taking the
value 0 or 1 with equal probabilities independently of the others.&lt;/blockquote&gt;&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The paper discusses many methods of testing (implemented in their
testing suite), a few are given below:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Statistical two-level statistical test&lt;/em&gt;
&lt;/p&gt;&lt;blockquote&gt;Several
authors have advocated and/or applied a two-level (or second-order)
procedure for testing RNGs [Fishman 1996; Knuth 1998; L’Ecuyer 1992;
Marsaglia 1985]. The idea is to generate N “independent” copies of Y ,
say
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;Y&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;,&lt;/mo&gt;&lt;mi&gt;.&lt;/mi&gt;&lt;mi&gt;.&lt;/mi&gt;&lt;mi&gt;.&lt;/mi&gt;&lt;mo&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;Y&lt;/mi&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;Y_1 , . . . , Y_N&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;,
by replicating the first-order test N times on disjoint subsequences of
the generator’s output. Let F be the theoretical distribution function
of Y under
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;msub&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;H_0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;.
If F is continuous, the transformed observations
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;true&quot; form=&quot;prefix&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;Y&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy=&quot;true&quot; form=&quot;postfix&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;U_1 = F (Y_1)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;,
. . . ,
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;true&quot; form=&quot;prefix&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;Y&lt;/mi&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy=&quot;true&quot; form=&quot;postfix&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;U_N = F (Y_N )&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;
are i.i.d.
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;true&quot; form=&quot;prefix&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo&gt;,&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo stretchy=&quot;true&quot; form=&quot;postfix&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;U (0, 1)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;
random variables under
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;msub&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;H_0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;
. One way of performing the two-level test is to compare the empirical
distribution of these
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;U_j&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;
’s to the uniform distribution, via a goodness-of-fit (GOF) test such as
those of Kolmogorov- Smirnov, Anderson-Darling, Crámer-von Mises, etc.
&lt;br&gt; […] &lt;br&gt; The p-value of the GOF test statistic is computed and
&lt;math display=&quot;inline&quot; xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;msub&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;H_0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;
is rejected if this p-value is deemed too extreme, as usual.&lt;/blockquote&gt;&lt;p&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Testing a stream of real numbers&lt;/em&gt;, this involves
“measuring global uniformity”, “measuring clustering” (if the numbers
tend to be clustered in a certain manner), and “run and gap tests”
(counts gap between successive values that land in a certain interval,
using the heuristic of Lebesgue measure).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;“Testing for n subsequences of length t”&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;!-- &lt;li&gt;&lt;p&gt;*Testing for&lt;/p&gt;&lt;/li&gt; --&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1805.01407&quot;&gt;Blackman and Vigna,
2018&lt;/a&gt; discuss some of the weaknesses of “F&lt;sub&gt;2&lt;/sub&gt;-linear
pseudorandom number generators”. They propose a new generator called &lt;a href=&quot;https://prng.di.unimi.it/&quot;&gt;xoshiro256**&lt;/a&gt; which supposedly
addresses the failures of previous RNGs including their own Xoshiro128+.
This in turn was criticized by &lt;a href=&quot;https://www.pcg-random.org/posts/a-quick-look-at-xoshiro256.html&quot;&gt;O’Neill,
2018&lt;/a&gt;, where she compares it to her own &lt;a href=&quot;https://www.pcg-random.org/paper.html&quot;&gt;PCG&lt;/a&gt; random number
generator algorithms.&lt;/p&gt;
&lt;!-- &lt;p&gt;work in progress to be continued-&lt;/p&gt;
&lt;p&gt;salt&lt;/p&gt; --&gt;
</content>
  </entry>
  <entry>
    <title>Functional programming, Lisp-like languages, and type theory</title>
    <link href="https://synkathairo.github.io/posts/2022-07-26-functional-lisp/" />
    <updated>2022-07-26T00:00:00Z</updated>
    <id>https://synkathairo.github.io/posts/2022-07-26-functional-lisp/</id>
    <content type="html">&lt;p&gt;I recently came across the concept of functional programming which
piqued my interest. The concept of functional programming syntactically
follows the convention as established by “Polish notation”; that is,
instead of putting operations between variables (eg. &lt;math&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt; to add 1
and 2), which reflects our linguistic conventions (especially in
subject-verb-object order) and the origins of mathematical expressions
from natural language grammars, we instead list the operation first and
then the variables or numbers involved, which is more logical for
computers.&lt;/p&gt;
&lt;p&gt;For example, to add 1 and 2 in Scheme, we write:&lt;/p&gt;
&lt;pre class=&quot;language-scheme&quot;&gt;&lt;code class=&quot;language-scheme&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which of course evaluates to 3.&lt;/p&gt;
&lt;p&gt;I found the SICP book (&lt;em&gt;Structure and Interpretation of Computer
Programs&lt;/em&gt;), linked below, to give a helpful introduction to Scheme.
The book utilizes the simplicity of Scheme to teach programming
concepts. Scheme, as a prominent Lisp derivative, in general follows the
convention of prefix notation-based syntax. The parentheses seem to
indicate a sort of order of operations as you might find in a
mathematical expression, and follows a simple syntactical logic, ie. to
do &lt;math&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;5&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mn&gt;6&lt;/mn&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;, we use:&lt;/p&gt;
&lt;pre class=&quot;language-scheme&quot;&gt;&lt;code class=&quot;language-scheme&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Functional programming languages correspond also mathematically to
the concepts of type theory and formal languages. Agda is a programming
language which has been used in mathematical proofs. Functional
programming is also widely used in some fields such as finance.&lt;/p&gt;
&lt;!-- &lt;p&gt;&lt;code&gt;TODO write more here&lt;/code&gt;&lt;/p&gt; --&gt;
&lt;p&gt;Resources:
&lt;/p&gt;&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Polish_notation&quot;&gt;Polish notation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Lisp_(programming_language)&quot;&gt;Lisp
programming language&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://staff.math.su.se/anders.mortberg/slides/master_slides.pdf&quot;&gt;Anders
Mörtberg - &lt;em&gt;Constructive Algebra in Functional Programming and Type
Theory&lt;/em&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://fmnt.info/blog/20181029_scheme.html&quot;&gt;Scheme for scientific
computing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://beautifulracket.com/&quot;&gt;Beautiful
Racket&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://staff.math.su.se/anders.mortberg/papers/cubicalagda.pdf&quot;&gt;Vezzozi,
Mörtberg, Abel - &lt;em&gt;Cubical Agda: A Dependently Typed Programming
Language with Univalence and Higher Inductive Types&lt;/em&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://ncatlab.org/nlab/show/cubical+type+theory&quot;&gt;Cubical type
theory&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://web.mit.edu/6.001/6.037/sicp.pdf&quot;&gt;Abelson &amp;amp; Sussman - &lt;em&gt;Structure and Interpretation of Computer Programs&lt;/em&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://wiki.portal.chalmers.se/agda/pmwiki.php&quot;&gt;Agda wiki&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.haskell.org/&quot;&gt;Haskell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://gambitscheme.org/&quot;&gt;Gambit Scheme&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.gnu.org/software/mit-scheme/&quot;&gt;MIT/GNU Scheme&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://racket-lang.org/&quot;&gt;Racket lang&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://hardmath123.github.io/perchance-to-scheme.html&quot;&gt;Perchance to Scheme&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
  </entry>
</feed>