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
#include "../../../defs.h"
/*==============================================================================
* taus.c
*
* Returns random unsigned chars as well as the new seeds.
*
* example: [rchar seeds] = taus(len, seeds)
*
* Author: Sebastian Wagner
* Date: 23-07-2012
*
===============================================================================*/
unsigned int s0, s1, s2, b;
inline unsigned int mtaus() {
b = (((s0 << 13) ^ s0) >> 19);
s0 = (((s0 & 0xFFFFFFFE) << 12)^ b);
b = (((s1 << 2) ^ s1) >> 25);
s1 = (((s1 & 0xFFFFFFF8) << 4)^ b);
b = (((s2 << 3) ^ s2) >> 11);
s2 = (((s2 & 0xFFFFFFF0) << 17)^ b);
return s0 ^ s1 ^ s2;
}
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]
)
{
/* Declare */
unsigned int len, *tseeds, *tseeds_out;
unsigned char *out;
int i;
if(nrhs!=2)
mexErrMsgTxt("Two inputs required.");
else if(nlhs > 2)
mexErrMsgTxt("Too many output arguments.");
/* Allocate input */
len = (unsigned int) mxGetScalar(prhs[0]);
tseeds = (unsigned int*) mxGetData(prhs[1]);
if (mxGetM(prhs[1])!=3 && mxGetN(prhs[1])!=3)
mexErrMsgTxt("Three seeds are required.");
s0 = tseeds[0];
s1 = tseeds[1];
s2 = tseeds[2];
/* Allocate Output */
plhs[0] = mxCreateNumericMatrix(len, 1, mxUINT8_CLASS, mxREAL);
out = (unsigned char*) mxGetPr(plhs[0]);
plhs[1] = mxCreateNumericMatrix(3, 1, mxUINT32_CLASS, mxREAL);
tseeds_out = (unsigned int*) mxGetPr(plhs[1]);
/* Algo */
for(i=0;i<len;i++)
out[i] = (unsigned char)(mtaus()&0xff);
tseeds_out[0] = s0;
tseeds_out[1] = s1;
tseeds_out[2] = s2;
}