1<html lang="en"> 2<head> 3<title>rand48 - Untitled</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Untitled"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Stdlib.html#Stdlib" title="Stdlib"> 9<link rel="prev" href="rand.html#rand" title="rand"> 10<link rel="next" href="strtod.html#strtod" title="strtod"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<meta http-equiv="Content-Style-Type" content="text/css"> 13<style type="text/css"><!-- 14 pre.display { font-family:inherit } 15 pre.format { font-family:inherit } 16 pre.smalldisplay { font-family:inherit; font-size:smaller } 17 pre.smallformat { font-family:inherit; font-size:smaller } 18 pre.smallexample { font-size:smaller } 19 pre.smalllisp { font-size:smaller } 20 span.sc { font-variant:small-caps } 21 span.roman { font-family:serif; font-weight:normal; } 22 span.sansserif { font-family:sans-serif; font-weight:normal; } 23--></style> 24<link rel="stylesheet" type="text/css" href="../cs.css"> 25</head> 26<body> 27<div class="node"> 28<a name="rand48"></a> 29<p> 30Next: <a rel="next" accesskey="n" href="strtod.html#strtod">strtod</a>, 31Previous: <a rel="previous" accesskey="p" href="rand.html#rand">rand</a>, 32Up: <a rel="up" accesskey="u" href="Stdlib.html#Stdlib">Stdlib</a> 33<hr> 34</div> 35 36<h3 class="section">2.33 <code>rand48</code>, <code>drand48</code>, <code>erand48</code>, <code>lrand48</code>, <code>nrand48</code>, <code>mrand48</code>, <code>jrand48</code>, <code>srand48</code>, <code>seed48</code>, <code>lcong48</code>—pseudo-random number generators and initialization routines</h3> 37 38<p><a name="index-rand48-69"></a><a name="index-drand48-70"></a><a name="index-erand48-71"></a><a name="index-lrand48-72"></a><a name="index-nrand48-73"></a><a name="index-mrand48-74"></a><a name="index-jrand48-75"></a><a name="index-srand48-76"></a><a name="index-seed48-77"></a><a name="index-lcong48-78"></a><strong>Synopsis</strong> 39<pre class="example"> #include <stdlib.h> 40 double drand48(void); 41 double erand48(unsigned short <var>xseed</var>[3]); 42 long lrand48(void); 43 long nrand48(unsigned short <var>xseed</var>[3]); 44 long mrand48(void); 45 long jrand48(unsigned short <var>xseed</var>[3]); 46 void srand48(long <var>seed</var>); 47 unsigned short *seed48(unsigned short <var>xseed</var>[3]); 48 void lcong48(unsigned short <var>p</var>[7]); 49 50</pre> 51 <p><strong>Description</strong><br> 52The <code>rand48</code> family of functions generates pseudo-random numbers 53using a linear congruential algorithm working on integers 48 bits in size. 54The particular formula employed is 55r(n+1) = (a * r(n) + c) mod m 56where the default values are 57for the multiplicand a = 0xfdeece66d = 25214903917 and 58the addend c = 0xb = 11. The modulo is always fixed at m = 2 ** 48. 59r(n) is called the seed of the random number generator. 60 61 <p>For all the six generator routines described next, the first 62computational step is to perform a single iteration of the algorithm. 63 64 <p><code>drand48</code> and <code>erand48</code> 65return values of type double. The full 48 bits of r(n+1) are 66loaded into the mantissa of the returned value, with the exponent set 67such that the values produced lie in the interval [0.0, 1.0]. 68 69 <p><code>lrand48</code> and <code>nrand48</code> 70return values of type long in the range 71[0, 2**31-1]. The high-order (31) bits of 72r(n+1) are loaded into the lower bits of the returned value, with 73the topmost (sign) bit set to zero. 74 75 <p><code>mrand48</code> and <code>jrand48</code> 76return values of type long in the range 77[-2**31, 2**31-1]. The high-order (32) bits of 78r(n+1) are loaded into the returned value. 79 80 <p><code>drand48</code>, <code>lrand48</code>, and <code>mrand48</code> 81use an internal buffer to store r(n). For these functions 82the initial value of r(0) = 0x1234abcd330e = 20017429951246. 83 84 <p>On the other hand, <code>erand48</code>, <code>nrand48</code>, and <code>jrand48</code> 85use a user-supplied buffer to store the seed r(n), 86which consists of an array of 3 shorts, where the zeroth member 87holds the least significant bits. 88 89 <p>All functions share the same multiplicand and addend. 90 91 <p><code>srand48</code> is used to initialize the internal buffer r(n) of 92<code>drand48</code>, <code>lrand48</code>, and <code>mrand48</code> 93such that the 32 bits of the seed value are copied into the upper 32 bits 94of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e. 95Additionally, the constant multiplicand and addend of the algorithm are 96reset to the default values given above. 97 98 <p><code>seed48</code> also initializes the internal buffer r(n) of 99<code>drand48</code>, <code>lrand48</code>, and <code>mrand48</code>, 100but here all 48 bits of the seed can be specified in an array of 3 shorts, 101where the zeroth member specifies the lowest bits. Again, 102the constant multiplicand and addend of the algorithm are 103reset to the default values given above. 104<code>seed48</code> returns a pointer to an array of 3 shorts which contains 105the old seed. 106This array is statically allocated, thus its contents are lost after 107each new call to <code>seed48</code>. 108 109 <p>Finally, <code>lcong48</code> allows full control over the multiplicand and 110addend used in <code>drand48</code>, <code>erand48</code>, <code>lrand48</code>, <code>nrand48</code>, 111<code>mrand48</code>, and <code>jrand48</code>, 112and the seed used in <code>drand48</code>, <code>lrand48</code>, and <code>mrand48</code>. 113An array of 7 shorts is passed as parameter; the first three shorts are 114used to initialize the seed; the second three are used to initialize the 115multiplicand; and the last short is used to initialize the addend. 116It is thus not possible to use values greater than 0xffff as the addend. 117 118 <p>Note that all three methods of seeding the random number generator 119always also set the multiplicand and addend for any of the six 120generator calls. 121 122 <p>For a more powerful random number generator, see <code>random</code>. 123 124 <p><br> 125<strong>Portability</strong><br> 126SUS requires these functions. 127 128 <p>No supporting OS subroutines are required. 129 130 <p><br> 131 132 </body></html> 133 134