<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/technology/cnxml/schema/dtd/0.5/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" xmlns:m="http://www.w3.org/1998/Math/MathML" id="new">
  <name>Streams of Pseudo-Random Numbers in the Functional Programming Language Haskell</name>
  <metadata>
  <md:version>1.1</md:version>
  <md:created>2006/04/02 15:12:55.370 GMT-5</md:created>
  <md:revised>2006/04/09 22:23:21.374 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="he_deceives">
      <md:firstname>Jacob</md:firstname>
      
      <md:surname>Schrum</md:surname>
      <md:email>he_deceives@hotmail.com</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="he_deceives">
      <md:firstname>Jacob</md:firstname>
      
      <md:surname>Schrum</md:surname>
      <md:email>he_deceives@hotmail.com</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>functional</md:keyword>
    <md:keyword>haskall</md:keyword>
    <md:keyword>lists</md:keyword>
    <md:keyword>numbers</md:keyword>
    <md:keyword>programming</md:keyword>
    <md:keyword>pseudo</md:keyword>
    <md:keyword>random</md:keyword>
    <md:keyword>streams</md:keyword>
  </md:keywordlist>

  <md:abstract>In developing an Artificial Life simulation, a pseudo-random number generator for the functional programming language Haskell is designed with complete code provided.</md:abstract>
</metadata>
  <content>
    <para id="element-193">Random numbers can be quite a controversial subject. One can argue that it is possible to predict everything given enough knowledge. That would mean that randomness is simply an abstraction for things we cannot understand, or for which there is not enough time or processing power to make carrying out the necessary calculations worthwhile, even if possible.</para><para id="delete_me">In computers the issue of randomness is somewhat less philosophical but still important. Strictly speaking, computers are completely deterministic. This means that generating random numbers is not possible. However, it is possible to generate pseudo-random numbers, which share many of the properties of random numbers and for nearly all applications are equally useful.</para><para id="element-95">In most imperative languages programmers have the option of creating their own random number generators with seeds, which are initial numbers that influence the series of random numbers created by the generator. Some languages simplify this further and use the value from the computer's internal clock as a seed. The problem with this practice is that one cannot replicate an experiment if the clock is used for seeding.</para><para id="element-871">In Haskell the deterministic nature of computers becomes even clearer. Haskell also has a built in random number generator, but because it is dependent upon the state of the computer it returns an IO type. This makes computations requiring a large number of random numbers difficult without the use of <code>unsafePerformIO</code>, which converts an IO type to a pure type. In the interest of maintaining the functional integrity of code, this function is not used. Instead, a custom random number generator taken from the book "The Craft of Functional Programming" by Simon Thompson (1999) is used.</para><para id="element-367">The method presented in Thompson's book is called the "linear congruential method," and all of the random number streams presented in this module stem from this method.</para><code type="block">&gt;module RandomStream
&gt;	(nextRandomIntegral,
&gt;	randomIntegrals, boundRandomIntegrals,
&gt;	smallRandomFloatings, boundRandomFloatings) where</code><para id="element-226">The three constants below come directly from Thompson's book, and each plays a role in the generation of pseudo-random numbers. The <code>nextRandomIntegral</code> function below takes an <code>Integral</code> value as input and multiplies it by the constant named <code>multiplier</code>. This value is then added to the <code>incrementer</code>, and modulus division by the constant <code>modulus</code> is performed on the result. The value <code>modulus</code> is important because it sets the range for the function. All numbers generated by this function have a range [0, <code>modulus</code>). The <code>modulus</code> is set to 65536, which is more than large enough for all applications to which this random number generator is applied.</para><code type="block">&gt;multiplier :: Num a =&gt; a
&gt;multiplier = 25173 

&gt;incrementer :: Num a =&gt; a
&gt;incrementer = 13849 

&gt;modulus :: Num a =&gt; a
&gt;modulus = 65536 </code><para id="element-268">According to Donald E. Knuth's book "The Art of Computer Programming Vol. 2: Seminumerical Algorithms," (1969) Thompson's choice of "magic numbers" is valid. Knuth's book deals with, among other things, the issue of pseudo-random number generation by computers. Knuth explains that the linear congruential method always creates a looping sequence of numbers, and while it would be desirable to have an infinite list of random numbers, one must instead settle for creating a sequence with as large a period as possible. The period is the number of values generated before looping occurs.</para><para id="element-767">The maximum possible value of the period is <code>modulus</code>, in which case every value in the sequence occurs exactly once before repeating. In order for the period to equal <code>modulus</code>, several conditions must be met, as according to the Theorem A in Knuth's book: </para>   

<list id="theoremA" type="enumerate">
 <item><code>incrementer</code> must be relatively prime to <code>modulus</code>.</item>
 <item>(<code>multiplier</code> - 1) is a multiple of p, for every prime p dividing <code>modulus</code></item>
 <item>(<code>multiplier</code> - 1) is a multiple of 4, if <code>modulus</code> is a multiple of 4.</item>

</list><para id="element-391">Thompson's numbers satisfy condition 1 because gcd(13849,65536) = 1. Condition 3 is satisfied because 4|(25173 - 1) and 4|65536. Proving condition 2 is more difficult, but thankfully Knuth provides a simpler test that can be used to assure that <code>multiplier</code> and <code>modulus</code> are chosen so that every value in the range [0,<code>modulus</code>) occurs before repeating. Since 65536 is a power of 2 (65536 = 2^16), all that must be done to assure that every number in the sequence occurs before repeating is to choose <code>multiplier</code> such that <code>multiplier</code> mod 8 = 5. Thompson's <code>incrementer</code> satisfies this property (25173 mod 8 = 5).</para><para id="element-26">One advantage of having the period equal <code>modulus</code> is that it does not matter what value is used to seed the generator. Since all values from 0 to (<code>modulus</code> - 1) appear in the sequence exactly once, any of these values can be used as a starting point. This is convenient because multiple copies of this generator are meant to be used simultaneously, so each needs to have a different starting seed in order to be independent from the others.</para><para id="element-570">Now on to the actual random number generator. Notice that the type signature of <code>nextRandomIntegral</code> allows for the generation of any general <code>Integral</code> type. This means the function works for both <code>Int</code> and <code>Integer</code> types, which is convenient since random numbers of both types are needed by the simulation.</para>

<list id="f1">
<name>Inputs</name>
<item>seed or previous random number</item>
</list><code type="block">&gt;nextRandomIntegral :: Integral a =&gt; a -&gt; a
&gt;nextRandomIntegral n = ((n * multiplier) + incrementer) `mod` modulus </code><para id="element-551">The <code>nextRandomIntegral</code> function takes a single value as input and generates another. In order to obtain many random numbers this function needs to be applied repeatedly, and Haskell happens to have a useful function named <code>iterate</code> which accomplishes this. Iterating with the <code>nextRandomIntegral</code> function means that the result of each application of <code>nextRandomIntegral</code> is itself used as the input to the next application of the function. The outputs are returned in a stream. This is possible because of Haskell's use of lazy evaluation. All that is needed is an initial value to start with, which is the seed of the random number generator. The seed is the input to the function <code>randomIntegrals</code> and different seeds create different random streams. According to Thompson the numbers in this sequence "all occur with the same frequency." (Thompson 1999, pg. 367)</para>




<list id="f2"><name>Inputs</name>
	<item>seed</item>
</list> <code type="block">&gt;randomIntegrals :: Integral a =&gt; a -&gt; [a]
&gt;randomIntegrals = iterate nextRandomIntegral </code><para id="element-14">Thompson also provides a method of restricting the range of this function to generate numbers in the range [s,t]. This is done with the <code>scaleSequence</code> function. It works by splitting the original range of numbers into "range blocks" and then mapping values in given ranges to new values. Unfortunately, it only works properly if the size of the new range is divisible by <code>modulus</code>. Otherwise it maps the last few values in the initial range [0, <code>modulus</code> - 1] to values outside of the desired range [s,t]. Therefore the function <code>boundRandomIntegrals</code> removes values out of range after applying <code>scaleSequence</code> to the initial stream of random numbers produced by <code>randomIntegrals</code>.</para>


<list id="f3"><name>Inputs</name>
<item> minimum value </item>
<item> maximum value </item>
<item> list of random integrals in range [0, <code>modulus</code> - 1] </item>

</list><code type="block">&gt;scaleSequence :: Integral a =&gt; a -&gt; a -&gt; [a] -&gt; [a]
&gt;scaleSequence s t = map scale
&gt;	where
&gt;		scale n = n `div` denom + s
&gt;	 	range = t - s + 1
&gt;	 	denom = modulus `div` range</code><para id="element-477">The <code>boundRandomIntegrals</code> function combines the <code>randomIntegrals</code> and <code>scaleSequence</code> functions into one simple function, and then removes the occasional value that is outside of the scaled range. All values within the desired range are equally likely to occur even though use of the <code>scaleSequence</code> function may result in some values being discarded. The probability of <code>scaleSequence</code> returning a value that needs to be discarded is slightly less than the probability of any particular valid value being returned. After invalid values are discarded the resulting distribution of possible random numbers is uniform. Calling this function with a desired range [a,b] generates a stream of numbers in this range. The seed for the random number generator is also needed.</para>

<list id="f4">
<name>Inputs</name>
<item> minimum value </item>
<item> maximum value </item>
<item> seed </item>
</list><code type="block">&gt;boundRandomIntegrals :: Integral a =&gt; a -&gt; a -&gt; a -&gt; [a]
&gt;boundRandomIntegrals a b n = filter (&lt;= b) (scaleSequence a b (randomIntegrals n)) </code><para id="element-784">Random floating point numbers are also needed in the simulation. Thompson does not provide a method for generating random floating point numbers, but it is quite easy to derive a floating point number generator from the integral number generator he provides. First a function named <code>smallRandomFloatings</code> that generates floating point numbers in the range [0,1] is defined. This generator is particularly useful on its own, and also serves as the basis for the function <code>boundRandomFloatings</code> further below. The <code>smallRandomFloatings</code> function works by dividing every number in the <code>randomIntegrals</code> sequence by the maximum possible value in that sequence, namely <code>modulus</code> - 1. This allows for <code>modulus</code> number of possible random numbers in the range [0,1], which is more than enough for the purposes of the simulation.</para>

<list id="f5"><name>Inputs</name>
	<item>seed</item>
</list> <code type="block">&gt;smallRandomFloatings :: Floating a =&gt; Integer -&gt; [a]
&gt;smallRandomFloatings n = map ((/ (modulus - 1)).fromInteger) (randomIntegrals n)</code><para id="element-503">Since random floating point numbers larger than 1 and less than 0 are also needed in the simulation we need a function that scales floating point numbers to this range. Unlike the <code>boundRandomIntegrals</code> function which scales the range of <code>randomIntegrals</code> down, the <code>boundRandomFloatings</code> function below scales the range of <code>smallRandomFloatings</code> up. It can map values from this function to any range, but it is important to remember that no matter what the range, there are still only <code>modulus</code> number of possible values. This means that the number generator below works best for small ranges in which high precision is not important. As such it is ideal for this simulation. However, this generator would not work well for generating floating point numbers in particularly large ranges, as increasing the range increases the space between consecutive values that this function is capable of generating. In fact, if the range is set to [0, <code>modulus</code> - 1] then the space between each possible value is exactly one. In other words, the usefulness of the function <code>boundRandomFloatings</code> decreases as the range increases. The function works by multiplying each value of the <code>smallRandomFloatings</code> stream by the range, thus creating a stream with values from 0 to a - b, where a is the minimum and b is the maximum. The values of this stream are then shifted by being added to the minimum value. This makes the smallest possible value equal to the minimum of the range, even if it was negative, and the greatest possible value equal to the maximum of the range.</para>




<list id="f6"><name>Inputs</name>
	<item> minimum value </item>
<item> maximum value </item>
<item> seed </item>
</list><code type="block">&gt;boundRandomFloatings :: Floating a =&gt; a -&gt; a -&gt; Integer -&gt; [a]
&gt;boundRandomFloatings lo hi n  = map ((+ lo).(* (hi - lo))) (smallRandomFloatings n) </code>




  </content>
  
</document>
