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