Return to BSD News archive
Xref: sserve comp.os.386bsd.development:601 comp.os.386bsd.bugs:593 Newsgroups: comp.os.386bsd.development,comp.os.386bsd.bugs Path: sserve!newshost.anu.edu.au!munnari.oz.au!news.Hawaii.Edu!ames!agate!howland.reston.ans.net!usc!cs.utexas.edu!uunet!email!mbirgmei From: mbirgmei@email.tuwien.ac.at (Martin BIRGMEIER) Subject: rand48 family Message-ID: <1993Apr28.085207.20635@email.tuwien.ac.at> Organization: Technical University of Vienna Date: Wed, 28 Apr 1993 08:52:07 GMT Lines: 440 I needed this (even though srandom() might be better); gleaned it from a SunOS manual page; derived the constants to use (those not described in that man page) by writing a few test programs. It now seems to output the same as the SunOS version. I tried to write it with portability in mind, though of course with these functions you have to have shorts of 16 bits and longs of 32 bits - oh well. No man pages yet, maybe I'll supply them at some later date. I don't know how well this is written or if something equivalent already exists, but anyway... Feel free to redistribute, but please keep the copyright notices. Martin ============================== cut here ============================== *** /usr/src/lib/libc/stdlib/Makefile.inc.RAND48_ORIG Mon Mar 22 15:40:23 1993 --- /usr/src/lib/libc/stdlib/Makefile.inc Mon Apr 19 11:38:58 1993 *************** *** 6,12 **** SRCS+= abort.c atexit.c atoi.c atol.c bsearch.c calloc.c div.c exit.c \ getenv.c getopt.c heapsort.c labs.c ldiv.c malloc.c multibyte.c \ putenv.c qsort.c radixsort.c rand.c random.c setenv.c strtol.c \ ! strtoul.c system.c .if (${MACHINE} == "hp300") SRCS+= abs.s atof.c --- 6,14 ---- SRCS+= abort.c atexit.c atoi.c atol.c bsearch.c calloc.c div.c exit.c \ getenv.c getopt.c heapsort.c labs.c ldiv.c malloc.c multibyte.c \ putenv.c qsort.c radixsort.c rand.c random.c setenv.c strtol.c \ ! strtoul.c system.c \ ! _rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \ ! mrand48.c nrand48.c seed48.c srand48.c .if (${MACHINE} == "hp300") SRCS+= abs.s atof.c *** /usr/src/lib/libc/stdlib/srand48.c.RAND48_ORIG Mon Apr 19 11:30:12 1993 --- /usr/src/lib/libc/stdlib/srand48.c Mon Apr 19 11:36:54 1993 *************** *** 0 **** --- 1,30 ---- + /* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + + #include "rand48.h" + + extern unsigned short _rand48_seed[3]; + extern unsigned short _rand48_mult[3]; + extern unsigned short _rand48_add; + + void + srand48(long seed) + { + _rand48_seed[0] = RAND48_SEED_0; + _rand48_seed[1] = (unsigned short) seed; + _rand48_seed[2] = (unsigned short) (seed >> 16); + _rand48_mult[0] = RAND48_MULT_0; + _rand48_mult[1] = RAND48_MULT_1; + _rand48_mult[2] = RAND48_MULT_2; + _rand48_add = RAND48_ADD; + } *** /usr/src/lib/libc/stdlib/seed48.c.RAND48_ORIG Mon Apr 19 11:30:12 1993 --- /usr/src/lib/libc/stdlib/seed48.c Mon Apr 19 11:36:52 1993 *************** *** 0 **** --- 1,36 ---- + /* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + + #include "rand48.h" + + extern unsigned short _rand48_seed[3]; + extern unsigned short _rand48_mult[3]; + extern unsigned short _rand48_add; + + unsigned short * + seed48(unsigned short xseed[3]) + { + static unsigned short sseed[3]; + + sseed[0] = _rand48_seed[0]; + sseed[1] = _rand48_seed[1]; + sseed[2] = _rand48_seed[2]; + _rand48_seed[0] = xseed[0]; + _rand48_seed[1] = xseed[1]; + _rand48_seed[2] = xseed[2]; + _rand48_mult[0] = RAND48_MULT_0; + _rand48_mult[1] = RAND48_MULT_1; + _rand48_mult[2] = RAND48_MULT_2; + _rand48_add = RAND48_ADD; + return sseed; + } *** /usr/src/lib/libc/stdlib/rand48.h.RAND48_ORIG Mon Apr 19 11:30:12 1993 --- /usr/src/lib/libc/stdlib/rand48.h Mon Apr 19 11:36:12 1993 *************** *** 0 **** --- 1,30 ---- + /* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + + #ifndef _RAND48_H_ + #define _RAND48_H_ + + #include <math.h> + #include <stdlib.h> + + void _dorand48 __P((unsigned short[3])); + + #define RAND48_SEED_0 (0x330e) + #define RAND48_SEED_1 (0xabcd) + #define RAND48_SEED_2 (0x1234) + #define RAND48_MULT_0 (0xe66d) + #define RAND48_MULT_1 (0xdeec) + #define RAND48_MULT_2 (0x0005) + #define RAND48_ADD (0x000b) + + #endif /* _RAND48_H_ */ *** /usr/src/lib/libc/stdlib/nrand48.c.RAND48_ORIG Mon Apr 19 11:30:12 1993 --- /usr/src/lib/libc/stdlib/nrand48.c Mon Apr 19 11:36:46 1993 *************** *** 0 **** --- 1,21 ---- + /* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + + #include "rand48.h" + + long + nrand48(unsigned short xseed[3]) + { + _dorand48(xseed); + return ((long) xseed[2] << 15) + ((long) xseed[1] >> 1); + } *** /usr/src/lib/libc/stdlib/mrand48.c.RAND48_ORIG Mon Apr 19 11:30:12 1993 --- /usr/src/lib/libc/stdlib/mrand48.c Mon Apr 19 11:36:44 1993 *************** *** 0 **** --- 1,23 ---- + /* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + + #include "rand48.h" + + extern unsigned short _rand48_seed[3]; + + long + mrand48(void) + { + _dorand48(_rand48_seed); + return ((long) _rand48_seed[2] << 16) + (long) _rand48_seed[1]; + } *** /usr/src/lib/libc/stdlib/lrand48.c.RAND48_ORIG Mon Apr 19 11:30:11 1993 --- /usr/src/lib/libc/stdlib/lrand48.c Mon Apr 19 11:36:41 1993 *************** *** 0 **** --- 1,23 ---- + /* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + + #include "rand48.h" + + extern unsigned short _rand48_seed[3]; + + long + lrand48(void) + { + _dorand48(_rand48_seed); + return ((long) _rand48_seed[2] << 15) + ((long) _rand48_seed[1] >> 1); + } *** /usr/src/lib/libc/stdlib/lcong48.c.RAND48_ORIG Mon Apr 19 11:30:11 1993 --- /usr/src/lib/libc/stdlib/lcong48.c Mon Apr 19 11:36:38 1993 *************** *** 0 **** --- 1,30 ---- + /* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + + #include "rand48.h" + + extern unsigned short _rand48_seed[3]; + extern unsigned short _rand48_mult[3]; + extern unsigned short _rand48_add; + + void + lcong48(unsigned short p[7]) + { + _rand48_seed[0] = p[0]; + _rand48_seed[1] = p[1]; + _rand48_seed[2] = p[2]; + _rand48_mult[0] = p[3]; + _rand48_mult[1] = p[4]; + _rand48_mult[2] = p[5]; + _rand48_add = p[6]; + } *** /usr/src/lib/libc/stdlib/jrand48.c.RAND48_ORIG Mon Apr 19 11:30:11 1993 --- /usr/src/lib/libc/stdlib/jrand48.c Mon Apr 19 11:36:35 1993 *************** *** 0 **** --- 1,21 ---- + /* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + + #include "rand48.h" + + long + jrand48(unsigned short xseed[3]) + { + _dorand48(xseed); + return ((long) xseed[2] << 16) + (long) xseed[1]; + } *** /usr/src/lib/libc/stdlib/erand48.c.RAND48_ORIG Mon Apr 19 11:30:11 1993 --- /usr/src/lib/libc/stdlib/erand48.c Mon Apr 19 11:36:31 1993 *************** *** 0 **** --- 1,23 ---- + /* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + + #include "rand48.h" + + double + erand48(unsigned short xseed[3]) + { + _dorand48(xseed); + return ldexp((double) xseed[0], -48) + + ldexp((double) xseed[1], -32) + + ldexp((double) xseed[2], -16); + } *** /usr/src/lib/libc/stdlib/drand48.c.RAND48_ORIG Mon Apr 19 11:30:11 1993 --- /usr/src/lib/libc/stdlib/drand48.c Mon Apr 19 11:36:26 1993 *************** *** 0 **** --- 1,22 ---- + /* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + + #include "rand48.h" + + extern unsigned short _rand48_seed[3]; + + double + drand48(void) + { + return erand48(_rand48_seed); + } *** /usr/src/lib/libc/stdlib/_rand48.c.RAND48_ORIG Mon Apr 19 11:30:11 1993 --- /usr/src/lib/libc/stdlib/_rand48.c Mon Apr 19 11:36:20 1993 *************** *** 0 **** --- 1,46 ---- + /* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + + #include "rand48.h" + + unsigned short _rand48_seed[3] = { + RAND48_SEED_0, + RAND48_SEED_1, + RAND48_SEED_2 + }; + unsigned short _rand48_mult[3] = { + RAND48_MULT_0, + RAND48_MULT_1, + RAND48_MULT_2 + }; + unsigned short _rand48_add = RAND48_ADD; + + void + _dorand48(unsigned short xseed[3]) + { + unsigned long accu; + unsigned short temp[2]; + + accu = (unsigned long) _rand48_mult[0] * (unsigned long) xseed[0] + + (unsigned long) _rand48_add; + temp[0] = (unsigned short) accu; /* lower 16 bits */ + accu >>= sizeof(unsigned short) * 8; + accu += (unsigned long) _rand48_mult[0] * (unsigned long) xseed[1] + + (unsigned long) _rand48_mult[1] * (unsigned long) xseed[0]; + temp[1] = (unsigned short) accu; /* middle 16 bits */ + accu >>= sizeof(unsigned short) * 8; + accu += _rand48_mult[0] * xseed[2] + _rand48_mult[1] * xseed[1] + _rand48_mult[2] * xseed[0]; + xseed[0] = temp[0]; + xseed[1] = temp[1]; + xseed[2] = (unsigned short) accu; + } *** /usr/src/include/stdlib.h.RAND48_ORIG Mon Mar 22 15:40:30 1993 --- /usr/src/include/stdlib.h Mon Apr 19 11:33:32 1993 *************** *** 95,100 **** --- 95,111 ---- int mbtowc __P((wchar_t *, const char *, size_t)); size_t wcstombs __P((char *, const wchar_t *, size_t)); + /* don't ask me where to put these -- MB XXX */ + double drand48 __P((void)); + double erand48 __P((unsigned short[3])); + long lrand48 __P((void)); + long nrand48 __P((unsigned short[3])); + long mrand48 __P((void)); + long jrand48 __P((unsigned short[3])); + void srand48 __P((long)); + unsigned short *seed48 __P((unsigned short[3])); + void lcong48 __P((unsigned short[7])); + #ifndef _ANSI_SOURCE void cfree __P((void *)); int putenv __P((const char *)); *** /usr/include/stdlib.h.RAND48_ORIG Mon Mar 22 15:40:30 1993 --- /usr/include/stdlib.h Mon Apr 19 11:34:13 1993 *************** *** 95,100 **** --- 95,111 ---- int mbtowc __P((wchar_t *, const char *, size_t)); size_t wcstombs __P((char *, const wchar_t *, size_t)); + /* don't ask me where to put these -- MB XXX */ + double drand48 __P((void)); + double erand48 __P((unsigned short[3])); + long lrand48 __P((void)); + long nrand48 __P((unsigned short[3])); + long mrand48 __P((void)); + long jrand48 __P((unsigned short[3])); + void srand48 __P((long)); + unsigned short *seed48 __P((unsigned short[3])); + void lcong48 __P((unsigned short[7])); + #ifndef _ANSI_SOURCE void cfree __P((void *)); int putenv __P((const char *)); ============================== cut here ==============================