Rev | Line | |
---|
[2197] | 1 | #pragma once
|
---|
| 2 | #include <stdlib.h>
|
---|
| 3 |
|
---|
| 4 | #define RND ((double)rand() / (double)RAND_MAX)
|
---|
| 5 |
|
---|
| 6 | //=============================================================
|
---|
| 7 | class Halton { // 1D Halton sorozat
|
---|
| 8 | //=============================================================
|
---|
| 9 | int base; // a szamrendszer alapja
|
---|
| 10 | double value; // az sorozat aktualis eleme
|
---|
| 11 | double inv_base; // a szamrendszer alapjanak reciproka
|
---|
| 12 | public:
|
---|
| 13 | void SetBase( int b ) { // alap beallitasa + sorozat inicializalasa
|
---|
| 14 | base = b;
|
---|
| 15 | inv_base = 1.0/base;
|
---|
| 16 | value = 0.0;
|
---|
| 17 | }
|
---|
| 18 | int GetBase( ) { return base; }
|
---|
| 19 | void Set( long i ) { // A sorozat i. elemenek szamitasa
|
---|
| 20 | double f = inv_base = 1.0/base;
|
---|
| 21 | value = 0.0;
|
---|
| 22 | while ( i > 0 ) {
|
---|
| 23 | value += f * (double)(i % base);
|
---|
| 24 | i /= base;
|
---|
| 25 | f *= inv_base;
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 | double Next( ) { // a sorozat soron kovetkezo elemenek szamitasa
|
---|
| 29 | double r = 1.0 - value - 0.0000000001;
|
---|
| 30 | if (inv_base < r) value += inv_base;
|
---|
| 31 | else {
|
---|
| 32 | double h = inv_base, hh;
|
---|
| 33 | do {
|
---|
| 34 | hh = h;
|
---|
| 35 | h *= inv_base;
|
---|
| 36 | } while ( h >= r );
|
---|
| 37 | value += hh + h - 1.0;
|
---|
| 38 | }
|
---|
| 39 | return value;
|
---|
| 40 | }
|
---|
| 41 | operator double( ) { return value; }
|
---|
| 42 | };
|
---|
| 43 |
|
---|
Note: See
TracBrowser
for help on using the repository browser.