Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "c.h"
00033
00034 #include <math.h>
00035
00036 #define RAND48_SEED_0 (0x330e)
00037 #define RAND48_SEED_1 (0xabcd)
00038 #define RAND48_SEED_2 (0x1234)
00039 #define RAND48_MULT_0 (0xe66d)
00040 #define RAND48_MULT_1 (0xdeec)
00041 #define RAND48_MULT_2 (0x0005)
00042 #define RAND48_ADD (0x000b)
00043
00044 static unsigned short _rand48_seed[3] = {
00045 RAND48_SEED_0,
00046 RAND48_SEED_1,
00047 RAND48_SEED_2
00048 };
00049 static unsigned short _rand48_mult[3] = {
00050 RAND48_MULT_0,
00051 RAND48_MULT_1,
00052 RAND48_MULT_2
00053 };
00054 static unsigned short _rand48_add = RAND48_ADD;
00055
00056
00057 static void
00058 _dorand48(unsigned short xseed[3])
00059 {
00060 unsigned long accu;
00061 unsigned short temp[2];
00062
00063 accu = (unsigned long) _rand48_mult[0] * (unsigned long) xseed[0] +
00064 (unsigned long) _rand48_add;
00065 temp[0] = (unsigned short) accu;
00066 accu >>= sizeof(unsigned short) * 8;
00067 accu += (unsigned long) _rand48_mult[0] * (unsigned long) xseed[1] +
00068 (unsigned long) _rand48_mult[1] * (unsigned long) xseed[0];
00069 temp[1] = (unsigned short) accu;
00070 accu >>= sizeof(unsigned short) * 8;
00071 accu += _rand48_mult[0] * xseed[2] + _rand48_mult[1] * xseed[1] + _rand48_mult[2] * xseed[0];
00072 xseed[0] = temp[0];
00073 xseed[1] = temp[1];
00074 xseed[2] = (unsigned short) accu;
00075 }
00076
00077
00078 double
00079 pg_erand48(unsigned short xseed[3])
00080 {
00081 _dorand48(xseed);
00082 return ldexp((double) xseed[0], -48) +
00083 ldexp((double) xseed[1], -32) +
00084 ldexp((double) xseed[2], -16);
00085 }
00086
00087 long
00088 pg_lrand48(void)
00089 {
00090 _dorand48(_rand48_seed);
00091 return ((long) _rand48_seed[2] << 15) + ((long) _rand48_seed[1] >> 1);
00092 }
00093
00094 void
00095 pg_srand48(long seed)
00096 {
00097 _rand48_seed[0] = RAND48_SEED_0;
00098 _rand48_seed[1] = (unsigned short) seed;
00099 _rand48_seed[2] = (unsigned short) (seed >> 16);
00100 _rand48_mult[0] = RAND48_MULT_0;
00101 _rand48_mult[1] = RAND48_MULT_1;
00102 _rand48_mult[2] = RAND48_MULT_2;
00103 _rand48_add = RAND48_ADD;
00104 }