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