diff options
| author | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2021-01-14 17:04:36 +0000 |
|---|---|---|
| committer | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2021-01-14 17:04:36 +0000 |
| commit | 39c59bfbf329794d8fe337bd9dff51600c5cbc3b (patch) | |
| tree | 814d24b34a8418885d535e1022a493008e5f0d29 | |
| parent | Expand makefile (diff) | |
| download | SyncRNG-39c59bfbf329794d8fe337bd9dff51600c5cbc3b.tar.gz SyncRNG-39c59bfbf329794d8fe337bd9dff51600c5cbc3b.zip | |
Pass state as doubles
This is what is used in the R interface as well and while not
a 100% sane, it does seem to work and preserves backwards
compatibility.
| -rw-r--r-- | src/_syncrng.c | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/src/_syncrng.c b/src/_syncrng.c index 5aaa177..bd1612a 100644 --- a/src/_syncrng.c +++ b/src/_syncrng.c @@ -122,43 +122,49 @@ static PyObject *_syncrng_seed(PyObject *self, PyObject *args) uint32_t seed; uint64_t *state = NULL; - if (!PyArg_ParseTuple(args, "k", &seed)) + PyObject *dblObj; + + if (!PyArg_ParseTuple(args, "O", &dblObj)) return NULL; + seed = (uint32_t) PyLong_AsLong(dblObj); lfsr113_seed(seed, &state); - PyObject *pystate = Py_BuildValue("[k, k, k, k]", - state[0], state[1], state[2], state[3]); + PyObject *pystate = Py_BuildValue("[d, d, d, d, d]", + (double) state[0], + (double) state[1], + (double) state[2], + (double) state[3], + -1.0); free(state); return pystate; } static PyObject *_syncrng_rand(PyObject *self, PyObject *args) { - uint32_t i, value, numints; - uint64_t *localstate; + int i; + uint32_t rand; + uint64_t *localstate = malloc(sizeof(uint64_t) * 4); PyObject *listObj; - PyObject *intObj; + PyObject *dblObj; if (!PyArg_ParseTuple(args, "O!", &PyList_Type, &listObj)) return NULL; - // we're just assuming you would never pass more than 4 values - localstate = malloc(sizeof(uint32_t)*5); - numints = PyList_Size(listObj); - for (i=0; i<numints; i++) { - intObj = PyList_GetItem(listObj, i); - value = (uint32_t) PyLong_AsLong(intObj); - localstate[i] = value; + for (i=0; i<4; i++) { + dblObj = PyList_GetItem(listObj, i); + localstate[i] = (uint64_t) PyFloat_AS_DOUBLE(dblObj); } - uint32_t rand = lfsr113(&localstate); - localstate[4] = rand; + rand = lfsr113(&localstate); - PyObject *pystate = Py_BuildValue("[k, k, k, k, k]", - localstate[0], localstate[1], localstate[2], - localstate[3], rand); + PyObject *pystate = Py_BuildValue("[d, d, d, d, d]", + (double) localstate[0], + (double) localstate[1], + (double) localstate[2], + (double) localstate[3], + (double) rand); free(localstate); return pystate; } |
