| 1 |
#include <R.h> |
| 2 |
#include <Rinternals.h> |
| 3 |
#include <stdint.h> |
| 4 |
|
| 5 |
SEXP rand_lcg(SEXP n, SEXP seed, SEXP a, SEXP c, SEXP m) { |
| 6 |
int len = asInteger(n); |
| 7 |
uint64_t s = (uint64_t) asReal(seed); |
| 8 |
uint64_t A = (uint64_t) asReal(a); |
| 9 |
uint64_t C = (uint64_t) asReal(c); |
| 10 |
uint64_t M = (uint64_t) asReal(m); |
| 11 |
|
| 12 |
SEXP result = PROTECT(allocVector(REALSXP, len)); |
| 13 |
double *x = REAL(result); |
| 14 |
|
| 15 |
for (int i = 0; i < len; i++) { |
| 16 |
s = (A * s + C) % M; |
| 17 |
x[i] = (double) s; |
| 18 |
} |
| 19 |
|
| 20 |
UNPROTECT(1); |
| 21 |
return result; |
| 22 |
} |