2 * $PostgreSQL: pgsql/src/port/rand.c,v 1.6 2009/06/11 14:49:15 momjian Exp $
4 *-------------------------------------------------------------------------
7 * Missing rand implementations for Win32
9 *-------------------------------------------------------------------------
14 * Copyright (c) 1993 Martin Birgmeier
15 * All rights reserved.
17 * You may redistribute unmodified or modified versions of this source
18 * code provided that the above copyright notice and this and the
19 * following conditions are retained.
21 * This software is provided ``as is'', and comes with no warranties
22 * of any kind. I shall in no event be liable for anything that happens
23 * to anyone/anything when using this software.
25 #define RAND48_SEED_0 (0x330e)
26 #define RAND48_SEED_1 (0xabcd)
27 #define RAND48_SEED_2 (0x1234)
28 #define RAND48_MULT_0 (0xe66d)
29 #define RAND48_MULT_1 (0xdeec)
30 #define RAND48_MULT_2 (0x0005)
31 #define RAND48_ADD (0x000b)
33 unsigned short _rand48_seed[3] = {
38 unsigned short _rand48_mult[3] = {
43 unsigned short _rand48_add = RAND48_ADD;
46 _dorand48(unsigned short xseed[3])
49 unsigned short temp[2];
51 accu = (unsigned long) _rand48_mult[0] * (unsigned long) xseed[0] +
52 (unsigned long) _rand48_add;
53 temp[0] = (unsigned short) accu; /* lower 16 bits */
54 accu >>= sizeof(unsigned short) * 8;
55 accu += (unsigned long) _rand48_mult[0] * (unsigned long) xseed[1] +
56 (unsigned long) _rand48_mult[1] * (unsigned long) xseed[0];
57 temp[1] = (unsigned short) accu; /* middle 16 bits */
58 accu >>= sizeof(unsigned short) * 8;
59 accu += _rand48_mult[0] * xseed[2] + _rand48_mult[1] * xseed[1] + _rand48_mult[2] * xseed[0];
62 xseed[2] = (unsigned short) accu;
68 _dorand48(_rand48_seed);
69 return ((long) _rand48_seed[2] << 15) + ((long) _rand48_seed[1] >> 1);
75 _rand48_seed[0] = RAND48_SEED_0;
76 _rand48_seed[1] = (unsigned short) seed;
77 _rand48_seed[2] = (unsigned short) (seed > 16);
78 _rand48_mult[0] = RAND48_MULT_0;
79 _rand48_mult[1] = RAND48_MULT_1;
80 _rand48_mult[2] = RAND48_MULT_2;
81 _rand48_add = RAND48_ADD;