blob: 2e70f8b122a3738a4bb0a21f347e2d20f2037780 (
plain)
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
69
70
71
72
73
74
75
76
77
78
79
|
SyncRNG
=======
A synchronized Tausworthe RNG usable in R and Python.
Why?
----
This program was created because it was desired to have the same random
numbers in both R and Python programs. Although both languages implement a
Mersenne-Twister RNG, the implementations are so different that it is not
possible to get the same random numbers with the same seed.
SyncRNG is a Tausworthe RNG implemented in `syncrng.c`, and linked to both R
and Python. Since both use the same underlying C code, the random numbers will
be the same in both languages, provided the same seed is used.
How
---
First install the packages as stated under Installation. Then, in a Python
script located in the same directory as `syncrng.so` and `SyncRNG.py`, you can
do:
```python
from SyncRNG import SyncRNG
s = SyncRNG(seed=123456)
for i in range(10):
print(s.randi())
```
Similarly, in an R script located in the same directory as `RSyncRNG.so` and
`SyncRNG.R`, you can do:
```R
library(SyncRNG)
s = SyncRNG(seed=123456)
for (i in 1:10) {
cat(s$randi(), '\n')
}
```
You'll notice that the random numbers are indeed the same.
Installation
------------
Installing the R package can be done through devtools:
```R
library(devtools)
devtools::install_github("GjjvdBurg/SyncRNG")
```
To install SyncRNG as a Python module, first clone the repository. The Python
module can then be installed locally for the user using:
```sh
python setup.py install --user
```
or system-wide through:
```sh
sudo python setup.py install
```
Notes
-----
The random numbers are uniformly distributed on `[0, 2^32 - 1]`.
TODO
----
It may be easier to provide system-wide installation through an R package and
a Python module.
|