SIMLIB/C++
3.09
Toggle main menu visibility
Loading...
Searching...
No Matches
src
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
33
SIMLIB_IMPLEMENTATION
;
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
//
88
static
double (*
SIMLIB_RandomBasePtr
)() =
SIMLIB_RandomBase
;
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
internal.h
Internal header file for SIMLIB/C++.
simlib3
Implementation of class CalendarList interface is static - using global functions in SQS namespace.
Definition
algloop.cc:32
simlib3::SIMLIB_RandomBase
double SIMLIB_RandomBase()
Definition
random1.cc:75
simlib3::myint32
long myint32
Definition
random1.cc:39
simlib3::SIGNBIT
const myint32 SIGNBIT
Definition
random1.cc:53
simlib3::SIMLIB_IMPLEMENTATION
SIMLIB_IMPLEMENTATION
Definition
algloop.cc:34
simlib3::INICONST
const myint32 INICONST
Definition
random1.cc:48
simlib3::MAXLONGINT
const myint32 MAXLONGINT
Definition
random1.cc:52
simlib3::SIMLIB_RandomSeed
static myint32 SIMLIB_RandomSeed
Definition
random1.cc:59
simlib3::MULCONST
const myint32 MULCONST
Definition
random1.cc:49
simlib3::SIMLIB_RandomBasePtr
static double(*) SIMLIB_RandomBasePtr()
Definition
random1.cc:88
simlib3::RandomSeed
void RandomSeed(long seed)
Definition
random1.cc:64
simlib3::Random
double Random()
Definition
random1.cc:95
simlib3::SetBaseRandomGenerator
void SetBaseRandomGenerator(double(*new_gen)())
Definition
random1.cc:103
simlib.h
Main SIMLIB/C++ interface.
Generated on
for SIMLIB/C++ by
1.18.0