1 | #include <math.h>
|
---|
2 | #include <stdlib.h>
|
---|
3 |
|
---|
4 | #include "randtmt64.h"
|
---|
5 |
|
---|
6 | // pas definit en C++ (seulement en C)
|
---|
7 | #ifndef UINT64_C
|
---|
8 | # define UINT64_C(v) (v ## ULL)
|
---|
9 | #endif
|
---|
10 | #include "tinymt64.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 | TinyMT64RandGen::TinyMT64RandGen(uint_8 seed)
|
---|
21 | {
|
---|
22 | // On est oblige de faire new car la structure tinymt64_t pas defini ds randtmt64.h
|
---|
23 | tinymt64_ptr_ = new tinymt64_t;
|
---|
24 | tinymt64_init(tinymt64_ptr_,seed);
|
---|
25 | }
|
---|
26 |
|
---|
27 | TinyMT64RandGen::~TinyMT64RandGen()
|
---|
28 | {
|
---|
29 | // Ne pas oublier de faire le delete
|
---|
30 | delete tinymt64_ptr_;
|
---|
31 | }
|
---|
32 |
|
---|
33 | void TinyMT64RandGen::ShowRandom()
|
---|
34 | {
|
---|
35 | cout<<"RandomGenerator is TinyMT64RandGen"<<endl;
|
---|
36 | }
|
---|
37 |
|
---|
38 | void TinyMT64RandGen::SetSeed(uint_8 seed)
|
---|
39 | {
|
---|
40 | tinymt64_init(tinymt64_ptr_,seed);
|
---|
41 | }
|
---|
42 |
|
---|
43 | void TinyMT64RandGen::SetSeed(vector<uint_8> seed)
|
---|
44 | {
|
---|
45 | if(seed.size()<=0) return;
|
---|
46 | int key_length = (int)seed.size();
|
---|
47 | uint_8* init_key = new uint_8[key_length];
|
---|
48 | for(int i=0;i<key_length;i++) init_key[i] = seed[i];
|
---|
49 |
|
---|
50 | tinymt64_init_by_array(tinymt64_ptr_,init_key,key_length);
|
---|
51 |
|
---|
52 | delete [] init_key;
|
---|
53 | }
|
---|
54 |
|
---|
55 | r_8 TinyMT64RandGen::Next()
|
---|
56 | {
|
---|
57 | return tinymt64_generate_double(tinymt64_ptr_);
|
---|
58 | }
|
---|
59 |
|
---|
60 | void TinyMT64RandGen::AutoInit(int lp)
|
---|
61 | // init automatique avec 2 mots de 64 bits
|
---|
62 | {
|
---|
63 | vector<uint_2> seed;
|
---|
64 | GenerateSeedVector(5,seed,lp);
|
---|
65 | vector<uint_8> s;
|
---|
66 | uint_8 s8;
|
---|
67 | s8 = uint_8(seed[0]) | (uint_8(seed[1])<<16) | (uint_8(seed[2])<<32) | (uint_8(seed[3])<<48);
|
---|
68 | s.push_back(s8);
|
---|
69 | s8 = uint_8(seed[4]) | (uint_8(seed[5])<<16) | (uint_8(seed[6])<<32) | (uint_8(seed[7])<<48);
|
---|
70 | s.push_back(s8);
|
---|
71 | SetSeed(s);
|
---|
72 | }
|
---|
73 |
|
---|
74 | //----------------------------------------------------------
|
---|
75 | // Classe pour la gestion de persistance
|
---|
76 | // ObjFileIO<TinyMT64RandGen>
|
---|
77 | //----------------------------------------------------------
|
---|
78 |
|
---|
79 | /* --Methode-- */
|
---|
80 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
81 | void ObjFileIO<TinyMT64RandGen>::WriteSelf(POutPersist& s) const
|
---|
82 | {
|
---|
83 | if (dobj == NULL)
|
---|
84 | throw NullPtrError("ObjFileIO<TinyMT64RandGen>::WriteSelf() dobj=NULL");
|
---|
85 | for(int i=0;i<2;i++)s.Put((uint_8)dobj->tinymt64_ptr_->status[i]);
|
---|
86 | s.Put((uint_4)dobj->tinymt64_ptr_->mat1);
|
---|
87 | s.Put((uint_4)dobj->tinymt64_ptr_->mat2);
|
---|
88 | s.Put((uint_8)dobj->tinymt64_ptr_->tmat);
|
---|
89 | return;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /* --Methode-- */
|
---|
93 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
94 | void ObjFileIO<TinyMT64RandGen>::ReadSelf(PInPersist& s)
|
---|
95 | {
|
---|
96 | if(dobj == NULL) dobj = new TinyMT64RandGen();
|
---|
97 | uint_4 v4; uint_8 v8;
|
---|
98 | for(int i=0;i<2;i++) {
|
---|
99 | s.Get(v8);
|
---|
100 | dobj->tinymt64_ptr_->status[i] = v8;
|
---|
101 | }
|
---|
102 | s.Get(v4); dobj->tinymt64_ptr_->mat1 = v4;
|
---|
103 | s.Get(v4); dobj->tinymt64_ptr_->mat2 = v4;
|
---|
104 | s.Get(v8); dobj->tinymt64_ptr_->tmat = v8;
|
---|
105 | return;
|
---|
106 | }
|
---|
107 |
|
---|
108 | // ---------------------------------------------------------
|
---|
109 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
110 | #pragma define_template ObjFileIO<TinyMT64RandGen>
|
---|
111 | #endif
|
---|
112 |
|
---|
113 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
114 | template class ObjFileIO<TinyMT64RandGen>;
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | } /* namespace SOPHYA */
|
---|