SIMLIB/C++  3.07
random1.cc
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 //! \file random1.cc Random number generators - basic
3 //
4 // Copyright (c) 1991-2007 Petr Peringer
5 //
6 // This library is licensed under GNU Library GPL. See the file COPYING.
7 //
8 
9 //
10 // base random number generator
11 // generate uniform distribution in the range 0 .. 0.99999999999....
12 //
13 
14 ////////////////////////////////////////////////////////////////////////////
15 // interface
16 //
17 
18 #include "simlib.h"
19 #include "internal.h"
20 
21 
22 ////////////////////////////////////////////////////////////////////////////
23 // implementation
24 //
25 
26 namespace simlib3 {
27 
28 // external functions
29 void RandomSeed(long seed); // initialize random number seed
30 double Random(); // base uniform generator 0-0.999999...
31 void SetBaseRandomGenerator(double (*new_gen)()); // change base gen.
32 
34 
35 
36 // TODO: change the basic generator to something better
37 
38 #if (LONG_MAX<(1ULL<<32))
39 typedef long myint32; // long has 32 bits
40 #else
41 typedef int myint32; // long has >32 bits
42 #endif
43 
44 ////////////////////////////////////////////////////////////////////////////
45 // some constants for base generator
46 //
47 
48 const myint32 INICONST = 1537L;
49 const myint32 MULCONST = 1220703125L;
50 // period = 536870912 only!
51 
52 const myint32 MAXLONGINT = 0x7FFFFFFFUL;
53 const myint32 SIGNBIT = 0x80000000UL;
54 
55 ////////////////////////////////////////////////////////////////////////////
56 // random generator seed
57 //
58 
59 static myint32 SIMLIB_RandomSeed = INICONST ;
60 
61 ////////////////////////////////////////////////////////////////////////////
62 // RandomSeed - initialization of random generator
63 //
64 void RandomSeed(long seed)
65 {
66  SIMLIB_RandomSeed = seed;
67 }
68 
69 ////////////////////////////////////////////////////////////////////////////
70 // SIMLIB_RandomBase --- default base uniform random number generator
71 //
72 // uses linear congruential method
73 // (not very good)
74 //
75 double SIMLIB_RandomBase() // range <0..1)
76 {
77  SIMLIB_RandomSeed *= MULCONST;
78  SIMLIB_RandomSeed &= MAXLONGINT; // strip sign bit
79 // _Print("random=%lx\n", (long)SIMLIB_RandomSeed);
80  double r = static_cast<double>(SIMLIB_RandomSeed)/MAXLONGINT;
81  // assert: if( r<0.0 || r>=1.0 ) SIMLIB_error("Random() out of range");
82  return r;
83 }
84 
85 ////////////////////////////////////////////////////////////////////////////
86 // pointer to base generator
87 //
89 
90 ////////////////////////////////////////////////////////////////////////////
91 // Random --- base uniform random number generator
92 //
93 // generate pseudorandom number in the range 0 .. 0.999999999999999999999
94 //
95 double Random()
96 {
97  return SIMLIB_RandomBasePtr();
98 }
99 
100 ////////////////////////////////////////////////////////////////////////////
101 // SetBaseRandomGenerator --- change base random number generator
102 //
103 void SetBaseRandomGenerator(double (*new_gen)())
104 {
105  if(new_gen)
106  SIMLIB_RandomBasePtr = new_gen;
107  else
108  SIMLIB_RandomBasePtr = SIMLIB_RandomBase; // default value
109 }
110 
111 }
112 // end
113 
double Random()
base uniform generator (range 0-0.999999...) the default implementation is simple LCG 32bit ...
Definition: random1.cc:95
long myint32
Definition: random1.cc:39
Implementation of class CalendarList interface is static - using global functions in SQS namespace...
Definition: algloop.cc:32
void RandomSeed(long seed)
initialize random number seed
Definition: random1.cc:64
const myint32 SIGNBIT
Definition: random1.cc:53
const myint32 INICONST
Definition: random1.cc:48
Internal header file for SIMLIB/C++.
Main SIMLIB/C++ interface.
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34
void SetBaseRandomGenerator(double(*new_gen)())
set another random generator default Random() implementation can be replaced
Definition: random1.cc:103
static myint32 SIMLIB_RandomSeed
Definition: random1.cc:59
static double(* SIMLIB_RandomBasePtr)()
Definition: random1.cc:88
const myint32 MAXLONGINT
Definition: random1.cc:52
double SIMLIB_RandomBase()
Definition: random1.cc:75
const myint32 MULCONST
Definition: random1.cc:49