Home | History | Annotate | Line # | Download | only in stdlib
_rand48.c revision 1.5
      1  1.5   lukem /*	$NetBSD: _rand48.c,v 1.5 1999/09/16 11:45:32 lukem Exp $	*/
      2  1.4   perry 
      3  1.1  brezak /*
      4  1.1  brezak  * Copyright (c) 1993 Martin Birgmeier
      5  1.1  brezak  * All rights reserved.
      6  1.1  brezak  *
      7  1.1  brezak  * You may redistribute unmodified or modified versions of this source
      8  1.1  brezak  * code provided that the above copyright notice and this and the
      9  1.1  brezak  * following conditions are retained.
     10  1.1  brezak  *
     11  1.1  brezak  * This software is provided ``as is'', and comes with no warranties
     12  1.1  brezak  * of any kind. I shall in no event be liable for anything that happens
     13  1.1  brezak  * to anyone/anything when using this software.
     14  1.1  brezak  */
     15  1.1  brezak 
     16  1.5   lukem #include <assert.h>
     17  1.5   lukem 
     18  1.1  brezak #include "rand48.h"
     19  1.1  brezak 
     20  1.3     jtc unsigned short __rand48_seed[3] = {
     21  1.1  brezak 	RAND48_SEED_0,
     22  1.1  brezak 	RAND48_SEED_1,
     23  1.1  brezak 	RAND48_SEED_2
     24  1.1  brezak };
     25  1.3     jtc unsigned short __rand48_mult[3] = {
     26  1.1  brezak 	RAND48_MULT_0,
     27  1.1  brezak 	RAND48_MULT_1,
     28  1.1  brezak 	RAND48_MULT_2
     29  1.1  brezak };
     30  1.3     jtc unsigned short __rand48_add = RAND48_ADD;
     31  1.1  brezak 
     32  1.1  brezak void
     33  1.2     jtc __dorand48(unsigned short xseed[3])
     34  1.1  brezak {
     35  1.1  brezak 	unsigned long accu;
     36  1.1  brezak 	unsigned short temp[2];
     37  1.5   lukem 
     38  1.5   lukem 	_DIAGASSERT(xseed != NULL);
     39  1.5   lukem #ifdef _DIAGNOSTIC
     40  1.5   lukem 	if (xseed == NULL)
     41  1.5   lukem 		return;
     42  1.5   lukem #endif
     43  1.1  brezak 
     44  1.3     jtc 	accu = (unsigned long) __rand48_mult[0] * (unsigned long) xseed[0] +
     45  1.3     jtc 	 (unsigned long) __rand48_add;
     46  1.1  brezak 	temp[0] = (unsigned short) accu;	/* lower 16 bits */
     47  1.1  brezak 	accu >>= sizeof(unsigned short) * 8;
     48  1.3     jtc 	accu += (unsigned long) __rand48_mult[0] * (unsigned long) xseed[1] +
     49  1.3     jtc 	 (unsigned long) __rand48_mult[1] * (unsigned long) xseed[0];
     50  1.1  brezak 	temp[1] = (unsigned short) accu;	/* middle 16 bits */
     51  1.1  brezak 	accu >>= sizeof(unsigned short) * 8;
     52  1.3     jtc 	accu += __rand48_mult[0] * xseed[2] + __rand48_mult[1] * xseed[1] + __rand48_mult[2] * xseed[0];
     53  1.1  brezak 	xseed[0] = temp[0];
     54  1.1  brezak 	xseed[1] = temp[1];
     55  1.1  brezak 	xseed[2] = (unsigned short) accu;
     56  1.1  brezak }
     57