@@ -103,7 +103,7 @@ void mexFunction (int nlhs, mxArray* plhs[],
103
103
mexErrMsgTxt (" The first input argument (N or X) must be of type double." );
104
104
}
105
105
// Second input argument (nboot)
106
- const int nboot = *(mxGetPr (prhs[1 ])); // 32-bit int
106
+ const int nboot = static_cast < const int > ( *(mxGetPr (prhs[1 ])) ); // 32-bit int
107
107
if ( mxGetNumberOfElements (prhs[1 ]) > 1 ) {
108
108
mexErrMsgTxt (" The second input argument (NBOOT) must be scalar." );
109
109
}
@@ -125,7 +125,7 @@ void mexFunction (int nlhs, mxArray* plhs[],
125
125
if (mxGetNumberOfElements (prhs[2 ]) > 1 || !mxIsClass (prhs[2 ], " logical" )) {
126
126
mexErrMsgTxt (" The third input argument (LOO) must be a logical scalar value." );
127
127
}
128
- loo = *(mxGetLogicals (prhs[2 ]));
128
+ loo = static_cast < bool > ( *(mxGetLogicals (prhs[2 ])) );
129
129
} else {
130
130
loo = false ;
131
131
}
@@ -138,11 +138,13 @@ void mexFunction (int nlhs, mxArray* plhs[],
138
138
if ( !mxIsClass (prhs[3 ], " double" ) ) {
139
139
mexErrMsgTxt (" The fourth input argument (SEED) must be of type double." );
140
140
}
141
- seed = *(mxGetPr (prhs[3 ]));
141
+ seed = static_cast < unsigned long int > ( *(mxGetPr (prhs[3 ])) );
142
142
if ( !mxIsFinite (seed) ) {
143
143
mexErrMsgTxt (" The fourth input argument (SEED) cannot be NaN or Inf." );
144
144
}
145
- srand (seed);
145
+ } else {
146
+ random_device rd;
147
+ seed = static_cast <unsigned int > ( rd () );
146
148
}
147
149
// Fifth input argument (w, weights)
148
150
// Error checking is handled later (see below in 'Declare variables' section)
@@ -156,12 +158,11 @@ void mexFunction (int nlhs, mxArray* plhs[],
156
158
mwSize dims[2 ] = {static_cast <mwSize>(n), static_cast <mwSize>(nboot)};
157
159
plhs[0 ] = mxCreateNumericArray (2 , dims,
158
160
mxDOUBLE_CLASS,
159
- mxREAL); // Prepare array for bootstrap sample indices
160
- long long int N = n * nboot; // Total counts of all sample indices
161
- long long int k; // Variable to store random number
162
- long long int d; // Counter for cumulative sum calculations
163
- vector<long long int > c; // Counter for each of the sample indices
164
- c.reserve (n);
161
+ mxREAL); // Prepare array for sample indices
162
+ long long unsigned int N = n * nboot; // Total counts of all sample indices
163
+ long long unsigned int k; // Variable to store random number
164
+ long long unsigned int d; // Counter for cumulative sum calculation
165
+ vector<long long int > c (n, nboot); // Counter for each of the sample indices
165
166
if ( nrhs > 4 && !mxIsEmpty (prhs[4 ]) ) {
166
167
// Assign user defined weights (counts)
167
168
if ( !mxIsClass (prhs[4 ], " double" ) ) {
@@ -182,17 +183,12 @@ void mexFunction (int nlhs, mxArray* plhs[],
182
183
if ( w[i] < 0 ) {
183
184
mexErrMsgTxt (" The fifth input argument (WEIGHTS) must contain only positive integers." );
184
185
}
185
- c. push_back ( w[i]) ; // Set each element in c to the specified weight
186
+ c[i] = w[i]; // Set each element in c to the specified weight
186
187
s += c[i];
187
188
}
188
189
if ( s != N ) {
189
190
mexErrMsgTxt (" The elements of WEIGHTS must sum to N * NBOOT." );
190
191
}
191
- } else {
192
- // Assign weights (counts) for uniform sampling
193
- for ( int i = 0 ; i < n ; i++ ) {
194
- c.push_back (nboot); // Set each element in c to nboot
195
- }
196
192
}
197
193
long long int m = 0 ; // Counter for LOO sample index r
198
194
int r = -1 ; // Sample index for LOO
@@ -201,8 +197,8 @@ void mexFunction (int nlhs, mxArray* plhs[],
201
197
double *ptr = (double *) mxGetData (plhs[0 ]);
202
198
203
199
// Initialize pseudo-random number generator (Mersenne Twister 19937)
204
- mt19937 rng (rand () );
205
- uniform_int_distribution<int > distr (0 , n - 1 );
200
+ mt19937_64 rng (seed );
201
+ uniform_int_distribution<long long unsigned int > distr (0 , n - 1 );
206
202
207
203
// Perform balanced sampling
208
204
for ( int b = 0 ; b < nboot ; b++ ) {
@@ -226,7 +222,7 @@ void mexFunction (int nlhs, mxArray* plhs[],
226
222
loo = false ;
227
223
}
228
224
}
229
- uniform_int_distribution<int > distk (0 , N - m - 1 );
225
+ uniform_int_distribution<long long unsigned int > distk (0 , N - m - 1 );
230
226
k = distk (rng);
231
227
d = c[0 ];
232
228
for ( int j = 0 ; j < n ; j++ ) {
0 commit comments