summaryrefslogtreecommitdiff
path: root/g_src/random.cpp
blob: 1059cb97cefcc0bcf28818148ad100f26e0f806b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "platform.h"
#include <string.h>
#include <math.h>
#include <iosfwd>
#include <iostream>
#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <fstream>
#include <zlib.h>

#include "svector.h"
using std::string;

#include "endian.h"

#include "files.h"

#include "enabler.h"

#include "textlines.h"

#include "basics.h"

#include "random.h"

extern int32_t basic_seed;
extern int mt_index[MT_BUFFER_NUM];
extern short mt_cur_buffer;
extern short mt_virtual_buffer;
extern uint32_t mt_buffer[MT_BUFFER_NUM][MT_LEN];

//public domain RNG stuff by Michael Brundage
	//with some modifications by me to handle more buffers

void mt_init()
{
	mt_cur_buffer=0;
	mt_virtual_buffer=0;

    mt_buffer[0][0]=GetTickCount();
    int i;
    for(i=1;i<MT_LEN;i++)
		{
		//2010: better init line from wikipedia, ultimate source unknown
		mt_buffer[0][i]=1812433253UL * (mt_buffer[0][i-1] ^ (mt_buffer[0][i-1]>>30)) + i;
		}
    mt_index[0]=MT_LEN*sizeof(uint32_t);

	int32_t j;
	for(j=0;j<20;j++)trandom_twist();
}

#define MT_IA           397
#define MT_IB           (MT_LEN - MT_IA)
#define UPPER_MASK      0x80000000
#define LOWER_MASK      0x7FFFFFFF
#define MATRIX_A        0x9908B0DF
#define TWIST(b,i,j)    ((b)[i] & UPPER_MASK) | ((b)[j] & LOWER_MASK)
#define MAGIC(s)        (((s)&1)*MATRIX_A)

uint32_t mt_trandom()
{
    uint32_t * b = mt_buffer[mt_cur_buffer];
    int idx = mt_index[mt_cur_buffer];
    uint32_t s;
    int i;
	
    if (idx == MT_LEN*sizeof(uint32_t))
    {
        idx = 0;
        i = 0;
        for (; i < MT_IB; i++) {
            s = TWIST(b, i, i+1);
            b[i] = b[i + MT_IA] ^ (s >> 1) ^ MAGIC(s);
        }
        for (; i < MT_LEN-1; i++) {
            s = TWIST(b, i, i+1);
            b[i] = b[i - MT_IB] ^ (s >> 1) ^ MAGIC(s);
        }
        
        s = TWIST(b, MT_LEN-1, 0);
        b[MT_LEN-1] = b[MT_IA-1] ^ (s >> 1) ^ MAGIC(s);
    }
    mt_index[mt_cur_buffer] = idx + sizeof(uint32_t);
     return *(uint32_t *)((unsigned char *)b + idx);
}

void trandom_twist()
{
    uint32_t * b = mt_buffer[mt_cur_buffer];
    uint32_t s;
    int i;
	
    i = 0;
    for (; i < MT_IB; i++) {
        s = TWIST(b, i, i+1);
        b[i] = b[i + MT_IA] ^ (s >> 1) ^ MAGIC(s);
    }
    for (; i < MT_LEN-1; i++) {
        s = TWIST(b, i, i+1);
        b[i] = b[i - MT_IB] ^ (s >> 1) ^ MAGIC(s);
    }
    
    s = TWIST(b, MT_LEN-1, 0);
    b[MT_LEN-1] = b[MT_IA-1] ^ (s >> 1) ^ MAGIC(s);
}

//back to my crap - tarn
void pop_trandom_uniform_seed()
{
	if(mt_virtual_buffer>0)mt_virtual_buffer--;
	mt_cur_buffer=mt_virtual_buffer;
	if(mt_cur_buffer>=MT_BUFFER_NUM)mt_cur_buffer=MT_BUFFER_NUM-1;
}

void push_trandom_uniform_seed(uint32_t newseed)
{
	mt_virtual_buffer++;
	mt_cur_buffer=mt_virtual_buffer;
	if(mt_cur_buffer>=MT_BUFFER_NUM)
		{
		mt_cur_buffer=MT_BUFFER_NUM-1;
		errorlog_string("Random Buffer Overload");
		}

    short i;

	uint32_t * b = mt_buffer[mt_cur_buffer];

	b[0]=newseed;
    for(i=1;i<MT_LEN;i++)
		{
		//2010: better init line from wikipedia, ultimate source unknown
		b[i]=1812433253UL * (b[i-1] ^ (b[i-1]>>30)) + i;
		}
    mt_index[mt_cur_buffer]=MT_LEN*sizeof(uint32_t);

	trandom_twist();
}

void push_trandom_double_seed(uint32_t newseed1,uint32_t newseed2)
{
	mt_virtual_buffer++;
	mt_cur_buffer=mt_virtual_buffer;
	if(mt_cur_buffer>=MT_BUFFER_NUM)
		{
		mt_cur_buffer=MT_BUFFER_NUM-1;
		errorlog_string("Random Buffer Overload");
		}

    short i;

	uint32_t * b = mt_buffer[mt_cur_buffer];

	b[0]=newseed1/2+newseed2/2;
    for(i=1;i<MT_LEN;i++)
		{
		b[i]=1812433253UL * (b[i-1] ^ (b[i-1]>>30)) + i;
		}
    mt_index[mt_cur_buffer]=MT_LEN*sizeof(uint32_t);

	trandom_twist();
}

void push_trandom_triple_seed(uint32_t newseed1,uint32_t newseed2,uint32_t newseed3)
{
	mt_virtual_buffer++;
	mt_cur_buffer=mt_virtual_buffer;
	if(mt_cur_buffer>=MT_BUFFER_NUM)
		{
		mt_cur_buffer=MT_BUFFER_NUM-1;
		errorlog_string("Random Buffer Overload");
		}

    short i;

	uint32_t * b = mt_buffer[mt_cur_buffer];

	b[0]=newseed1/3+newseed2/3+newseed3/3;
    for(i=1;i<MT_LEN;i++)
		{
		b[i]=1812433253UL * (b[i-1] ^ (b[i-1]>>30)) + i;
		}
    mt_index[mt_cur_buffer]=MT_LEN*sizeof(uint32_t);

	trandom_twist();
}

//picks a random number from 0 to max-1
int32_t basic_random(int32_t max)
{
	r_num();

	return (int32_t)((uint32_t)basic_seed/((1073741824UL/(uint32_t)max)+1UL));
}

//sets seed to a random number from 0 to 1 billion
void r_num()
{
	basic_seed=(int32_t)(((uint32_t)basic_seed*907725UL+99979777UL)%1073741824UL);
}