resolved: NXC Mersenne Twister RNG for NXT rand with srand
Posted: 23 Jun 2011, 09:53
I don't understand this error msg:
this is the original ANSI C code:
this is the NXC pseudo C code:# Error: Line type "Code" is not valid while in the "Data Segment" state
File "c:\Temp\temp.nxc" ; line 7
# index sword
#----------------------------------------------------------
# Error: Invalid variable argument: index
File "c:\Temp\temp.nxc" ; line 13
# mov index, __D0InitTT800
#----------------------------------------------------------
# Error: Error parsing expression: index
File "c:\Temp\temp.nxc" ; line 18
#
#----------------------------------------------------------
# Error: Error parsing expression: index
File "c:\Temp\temp.nxc" ; line 20
#
#----------------------------------------------------------
# Error: Invalid variable argument: index
File "c:\Temp\temp.nxc" ; line 34
# mov index, __D0TT800
#----------------------------------------------------------
# Error: Error parsing expression: index
File "c:\Temp\temp.nxc" ; line 36
#
#----------------------------------------------------------
# Error: Error parsing expression: index
File "c:\Temp\temp.nxc" ; line 36
#
#----------------------------------------------------------
# Error: Invalid variable argument: index
File "c:\Temp\temp.nxc" ; line 36
# add index, __constVal0, __constVal1
#----------------------------------------------------------
8 errors during compilation
Code: Select all
int N;
int M;
unsigned long A[2];
unsigned long y[25];
int index;
void InitTT800() {
N = 25;
M = 7;
A[0] = 0;
A[1] = 0x8ebfd028;
index = N+1;
}
unsigned long TT800(void) {
unsigned long r, s, e;
int k;
if (index >= N) {
if (index > N) {
r = 9;
s = 3402;
for (k=0 ; k<N ; ++k) {
r = 509845221 * r + 3;
s *= s + 1;
y[k] = s + (r >> 10);
}
}
for (k=0 ; k<N-M ; ++k)
y[k] = y[k+M] ^ (y[k] >> 1) ^ A[y[k] & 1];
for (; k<N ; ++k)
y[k] = y[k+(M-N)] ^ (y[k] >> 1) ^ A[y[k] & 1];
index = 0;
}
e = y[index++];
e ^= (e << 7) & 0x2b5b2500;
e ^= (e << 15) & 0xdb8b0000;
e ^= (e >> 16);
return e;
}
task main() {
long rd;
InitTT800();
rd = TT800();
NumOut(0,48, rd);
rd = TT800();
NumOut(0,40, rd);
rd = TT800();
NumOut(0,32, rd);
rd = TT800();
NumOut(0,24, rd);
rd = TT800();
NumOut(0,16, rd);
while(1);
}
Code: Select all
unsigned TT800(void) {
const int N = 25;
const int M = 7;
const unsigned A[2] = { 0, 0x8ebfd028 };
static unsigned y[25];
static int index = N+1;
if (index >= N) {
int k;
if (index > N) {
unsigned r = 9, s = 3402;
for (k=0 ; k<N ; ++k) {
r = 509845221 * r + 3;
s *= s + 1;
y[k] = s + (r >> 10);
}
}
for (k=0 ; k<N-M ; ++k)
y[k] = y[k+M] ^ (y[k] >> 1) ^ A[y[k] & 1];
for (; k<N ; ++k)
y[k] = y[k+(M-N)] ^ (y[k] >> 1) ^ A[y[k] & 1];
index = 0;
}
unsigned e = y[index++];
e ^= (e << 7) & 0x2b5b2500;
e ^= (e << 15) & 0xdb8b0000;
e ^= (e >> 16);
return e;
}