source: Sophya/trunk/SophyaLib/BaseTools/randtmt64.cc@ 4057

Last change on this file since 4057 was 4021, checked in by cmv, 14 years ago

pb de conversion de uint_8* -> const uint64_t *, cmv 24/09/2011

File size: 3.1 KB
Line 
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
12namespace 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
20TinyMT64RandGen::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
27TinyMT64RandGen::~TinyMT64RandGen()
28{
29// Ne pas oublier de faire le delete
30 delete tinymt64_ptr_;
31}
32
33void TinyMT64RandGen::ShowRandom()
34{
35 cout<<"RandomGenerator is TinyMT64RandGen"<<endl;
36}
37
38void TinyMT64RandGen::SetSeed(uint_8 seed)
39{
40 tinymt64_init(tinymt64_ptr_,seed);
41}
42
43void 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_,(const uint64_t *)(init_key),key_length);
51
52 delete [] init_key;
53}
54
55r_8 TinyMT64RandGen::Next()
56{
57 return tinymt64_generate_double(tinymt64_ptr_);
58}
59
60void 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-- */
80DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
81void 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-- */
93DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
94void 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)
114template class ObjFileIO<TinyMT64RandGen>;
115#endif
116
117} /* namespace SOPHYA */
Note: See TracBrowser for help on using the repository browser.