1 | #include <math.h>
|
---|
2 | #include <stdlib.h>
|
---|
3 |
|
---|
4 | #include "randtmt32.h"
|
---|
5 |
|
---|
6 | // pas definit en C++ (seulement en C)
|
---|
7 | #ifndef UINT32_C
|
---|
8 | # define UINT32_C(v) (v ## UL)
|
---|
9 | #endif
|
---|
10 | #include "tinymt32.h"
|
---|
11 |
|
---|
12 | namespace SOPHYA {
|
---|
13 |
|
---|
14 | //----------------------------------------------------------------
|
---|
15 | // Implementation d'un generateur aleatoire utilisant le code
|
---|
16 | // Tiny Mersenne Twister (TinyMT).
|
---|
17 | // http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
|
---|
18 |
|
---|
19 |
|
---|
20 | TinyMT32RandGen::TinyMT32RandGen(uint_4 seed)
|
---|
21 | {
|
---|
22 | // On est oblige de faire new car la structure tinymt32_t pas defini ds randtmt32.h
|
---|
23 | tinymt32_ptr_ = new tinymt32_t;
|
---|
24 | tinymt32_init(tinymt32_ptr_,seed);
|
---|
25 | }
|
---|
26 |
|
---|
27 | TinyMT32RandGen::~TinyMT32RandGen()
|
---|
28 | {
|
---|
29 | // Ne pas oublier de faire le delete
|
---|
30 | delete tinymt32_ptr_;
|
---|
31 | }
|
---|
32 |
|
---|
33 | void TinyMT32RandGen::ShowRandom()
|
---|
34 | {
|
---|
35 | cout<<"RandomGenerator is TinyMT32RandGen"<<endl;
|
---|
36 | }
|
---|
37 |
|
---|
38 | void TinyMT32RandGen::SetSeed(uint_4 seed)
|
---|
39 | {
|
---|
40 | tinymt32_init(tinymt32_ptr_,seed);
|
---|
41 | }
|
---|
42 |
|
---|
43 | void TinyMT32RandGen::SetSeed(vector<uint_4> seed)
|
---|
44 | {
|
---|
45 | if(seed.size()<=0) return;
|
---|
46 | int key_length = (int)seed.size();
|
---|
47 | uint_4* init_key = new uint_4[key_length];
|
---|
48 | for(int i=0;i<key_length;i++) init_key[i] = seed[i];
|
---|
49 |
|
---|
50 | tinymt32_init_by_array(tinymt32_ptr_,init_key,key_length);
|
---|
51 |
|
---|
52 | delete [] init_key;
|
---|
53 | }
|
---|
54 |
|
---|
55 | r_8 TinyMT32RandGen::Next()
|
---|
56 | {
|
---|
57 | return (r_8)tinymt32_generate_float(tinymt32_ptr_);
|
---|
58 | }
|
---|
59 |
|
---|
60 | void TinyMT32RandGen::AutoInit(int lp)
|
---|
61 | // init automatique avec 2 mots de 32 bits
|
---|
62 | {
|
---|
63 | vector<uint_2> seed;
|
---|
64 | GenerateSeedVector(1,seed,lp);
|
---|
65 | vector<uint_4> s;
|
---|
66 | uint_4 s4;
|
---|
67 | s4 = uint_4(seed[0]) | (uint_4(seed[1])<<16);
|
---|
68 | s.push_back(s4);
|
---|
69 | s4 = uint_4(seed[2]) | (uint_4(seed[3])<<16);
|
---|
70 | s.push_back(s4);
|
---|
71 | SetSeed(s);
|
---|
72 | }
|
---|
73 |
|
---|
74 | //----------------------------------------------------------
|
---|
75 | // Classe pour la gestion de persistance
|
---|
76 | // ObjFileIO<TinyMT32RandGen>
|
---|
77 | //----------------------------------------------------------
|
---|
78 |
|
---|
79 | /* --Methode-- */
|
---|
80 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
81 | void ObjFileIO<TinyMT32RandGen>::WriteSelf(POutPersist& s) const
|
---|
82 | {
|
---|
83 | if (dobj == NULL)
|
---|
84 | throw NullPtrError("ObjFileIO<TinyMT32RandGen>::WriteSelf() dobj=NULL");
|
---|
85 | for(int i=0;i<4;i++)s.Put((uint_4)dobj->tinymt32_ptr_->status[i]);
|
---|
86 | s.Put((uint_4)dobj->tinymt32_ptr_->mat1);
|
---|
87 | s.Put((uint_4)dobj->tinymt32_ptr_->mat2);
|
---|
88 | s.Put((uint_4)dobj->tinymt32_ptr_->tmat);
|
---|
89 | return;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /* --Methode-- */
|
---|
93 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
94 | void ObjFileIO<TinyMT32RandGen>::ReadSelf(PInPersist& s)
|
---|
95 | {
|
---|
96 | if(dobj == NULL) dobj = new TinyMT32RandGen();
|
---|
97 | uint_4 v;
|
---|
98 | for(int i=0;i<4;i++) {
|
---|
99 | s.Get(v);
|
---|
100 | dobj->tinymt32_ptr_->status[i] = v;
|
---|
101 | }
|
---|
102 | s.Get(v); dobj->tinymt32_ptr_->mat1 = v;
|
---|
103 | s.Get(v); dobj->tinymt32_ptr_->mat2 = v;
|
---|
104 | s.Get(v); dobj->tinymt32_ptr_->tmat = v;
|
---|
105 | return;
|
---|
106 | }
|
---|
107 |
|
---|
108 | // ---------------------------------------------------------
|
---|
109 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
110 | #pragma define_template ObjFileIO<TinyMT32RandGen>
|
---|
111 | #endif
|
---|
112 |
|
---|
113 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
114 | template class ObjFileIO<TinyMT32RandGen>;
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | } /* namespace SOPHYA */
|
---|