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

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

ajout generateurs tinymt32+64, cmv 24/09/2011

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