Generate the same random numbers in R and Python
Project description
SyncRNG
Generate the same random numbers in R and Python.
Why?
This program was created because I wanted to have the same random numbers in both R and Python programs. Although both languages implement a Mersenne-Twister random number generator (RNG), the implementations are so different that it is not possible to get the same random numbers, even with the same seed.
SyncRNG is a "Tausworthe" RNG implemented in 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 when the same seed is used.
You can read more about my motivations for creating this here.
Installation
Installing the R package can be done through CRAN:
> install.packages('SyncRNG')
The Python package can be installed using pip:
$ pip install syncrng
Usage
After installing the package, you can use the basic SyncRNG
random number
generator. In Python you can do:
>>> from SyncRNG import SyncRNG
>>> s = SyncRNG(seed=123456)
>>> for i in range(10):
>>> print(s.randi())
And in R you can use:
> 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.
R: User defined RNG
R allows the user to define a custom random number generator, which is then
used for the common runif
and rnorm
functions in R. This has also been
implemented in SyncRNG as of version 1.3.0. To enable this, run:
> library(SyncRNG)
> set.seed(123456, 'user', 'user')
> runif(10)
These numbers are between [0, 1) and multiplying by 2**32 - 1
gives the
same results as above.
Functionality
In both R and Python the following methods are available for the SyncRNG
class:
randi()
: generate a random integer on the interval [0, 2^32).rand()
: generate a random floating point number on the interval [0.0, 1.0)randbelow(n)
: generate a random integer below a given integern
.shuffle(x)
: generate a permutation of a given list of numbersx
.
Functionality is deliberately kept minimal to make maintaining this library easier. It is straightforward to build more advanced applications on the existing methods, as the following example shows.
Creating the same train/test splits
A common use case for this package is to create the same train and test splits
in R and Python. Below are some code examples that illustrate how to do this.
Both assume you have a matrix X
with 100
rows.
In R:
# This function creates a list with train and test indices for each fold
k.fold <- function(n, K, shuffle=TRUE, seed=0)
{
idxs <- c(1:n)
if (shuffle) {
rng <- SyncRNG(seed=seed)
idxs <- rng$shuffle(idxs)
}
# Determine fold sizes
fsizes <- c(1:K)*0 + floor(n / K)
mod <- n %% K
if (mod > 0)
fsizes[1:mod] <- fsizes[1:mod] + 1
out <- list(n=n, num.folds=K)
current <- 1
for (f in 1:K) {
fs <- fsizes[f]
startidx <- current
stopidx <- current + fs - 1
test.idx <- idxs[startidx:stopidx]
train.idx <- idxs[!(idxs %in% test.idx)]
out$testidxs[[f]] <- test.idx
out$trainidxs[[f]] <- train.idx
current <- stopidx
}
return(out)
}
# Which you can use as follows
folds <- k.fold(nrow(X), K=10, shuffle=T, seed=123)
for (f in 1:folds$num.folds) {
X.train <- X[folds$trainidx[[f]], ]
X.test <- X[folds$testidx[[f]], ]
# continue using X.train and X.test here
}
And in Python:
def k_fold(n, K, shuffle=True, seed=0):
"""Generator for train and test indices"""
idxs = list(range(n))
if shuffle:
rng = SyncRNG(seed=seed)
idxs = rng.shuffle(idxs)
fsizes = [n // K]*K
mod = n % K
if mod > 0:
fsizes[:mod] = [x+1 for x in fsizes[:mod]]
current = 0
for fs in fsizes:
startidx = current
stopidx = current + fs
test_idx = idxs[startidx:stopidx]
train_idx = [x for x in idxs if not x in test_idx]
yield train_idx, test_idx
current = stopidx
# Which you can use as follows
kf = k_fold(X.shape[0], K=3, shuffle=True, seed=123)
for trainidx, testidx in kf:
X_train = X[trainidx, :]
X_test = X[testidx, :]
# continue using X_train and X_test here
Notes
The random numbers are uniformly distributed on [0, 2^32 - 1]
. No
attention has been paid to thread-safety and you shouldn't use this random
number generator for cryptographic applications.
Questions and Issues
If you have questions, comments, or suggestions about SyncRNG or you encounter
a problem, please open an issue on
GitHub. Please don't hesitate to
contact me, you're helping to make this project better for everyone! If you
prefer not to use Github you can email me at gertjanvandenburg at gmail dot com
.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for SyncRNG-1.3.2-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4931397832535255e472d467ea0c97bcacd8cbea0b44cbacf8bbbff206e16b36 |
|
MD5 | 483b715263d91ac883a1109744d03763 |
|
BLAKE2b-256 | e46ce2a5ea8aded8f9c50d3f3e8ff641834f9995f122767589e30991aea55795 |
Hashes for SyncRNG-1.3.2-cp39-cp39-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f858e5943877d7a83805149d6e704736368f3513d81c06299281c8829388d77 |
|
MD5 | 1c015fdedf7de74002c77f0d3b5fafbe |
|
BLAKE2b-256 | 1dc92ad750073042b187d7d65e1dac44d6cfc887428b125636ca860a83a009de |
Hashes for SyncRNG-1.3.2-cp39-cp39-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e6158ddcbf60af2906dd9a417f27f982099f1b1e260d7bd03d7035dae3ad838 |
|
MD5 | 9532ed9e62b54b5ff15e457a53d08db9 |
|
BLAKE2b-256 | a00b781b195ab51b10d9996ecc4756f907a35ead0a434cb5cf4a15231299297b |
Hashes for SyncRNG-1.3.2-cp39-cp39-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 434daf3a10d0b4882ede6a64fe91031d013e4ac25e96d121277e96deb6c1d484 |
|
MD5 | fad3f616e3da870dea4bdad9b57fab00 |
|
BLAKE2b-256 | 7cfe9decabcc31b01f657837b29e3b90ce4b7da7ebaa79ba91de2d2f25f85fe5 |
Hashes for SyncRNG-1.3.2-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7da2d94007be54bd789cc2c358f5ef91995abdda5ea4f53e836358f08a796c9e |
|
MD5 | 78b0b7c508bf2b4f206e676d2b634b5a |
|
BLAKE2b-256 | 3a1446180e75d408ecd3823669d33c9b9df3b3daba98b94db1cdfa164c33afea |
Hashes for SyncRNG-1.3.2-cp39-cp39-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4222f2209adb526ae5985042e763b188faaf5786a662545e2ef14c114cd0a429 |
|
MD5 | b77814ea177892ae718a872f0314f8e9 |
|
BLAKE2b-256 | 28c6598a01bcde8b0a602d6a936159e89322e822137efdb71b5eba7e21075420 |
Hashes for SyncRNG-1.3.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b4f19f6d899552a071faf27fb7f5c77fc24d1351035c8deeabbbb4c27812ecc |
|
MD5 | fccd24d8133b4a8bd1e2b3ca4339813b |
|
BLAKE2b-256 | 957a60e98a82923f23beedc1bbf2af9106f8454b6ac9df632304c4ea88437f88 |
Hashes for SyncRNG-1.3.2-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ea140ab402ba5b87fd160ebeacdf6ac26ada667c18072614f0e7fae296ffed2 |
|
MD5 | cbd4db4904cd3a8d4f92ee3b78d4c0da |
|
BLAKE2b-256 | 3251ef75c870ce19ea89fe8bb92696947ee6a57d233f70830b8a93d672566dca |
Hashes for SyncRNG-1.3.2-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e4817599872b70e035ab222e564d078ef8d26d9819d68dea7e61c5d76533974 |
|
MD5 | 4052ed4528ca1edfacb542d7154d621b |
|
BLAKE2b-256 | d7666018832303034ba050ebfa07b2a59d0128484cea49a682a79149a7f6ab83 |
Hashes for SyncRNG-1.3.2-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e3a7afa01b1a3b329e347a6ee7177d6c546ebbff2c27d2aa656c93f27dac010 |
|
MD5 | cd373779f762f66fd4409b80a326a71e |
|
BLAKE2b-256 | e40dd2bb1c2aec480b0bc6e800e35f8ffee757cc700915ae68b3af7207094f22 |
Hashes for SyncRNG-1.3.2-cp38-cp38-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4594eb73a1ed4044d1bc83f48bdaf96c7ca6e57eaecc48151b2b3a8bb30d3b46 |
|
MD5 | cbba81398ea4ebc5cc9004e811c3a2e2 |
|
BLAKE2b-256 | 475d89febce06758d15689dd525ee833cfc57efd30d95d87001d9fa90f275160 |
Hashes for SyncRNG-1.3.2-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91ba7cc519d3fcbb45d6dce8146120c70ebc7452042bdadb2e550928f8799935 |
|
MD5 | d5d22926dea8d476af2427425c873527 |
|
BLAKE2b-256 | d0bd552af89b8342610c39fc45b7071737d1f57c8bb34a3f15201c2c3c2b4514 |
Hashes for SyncRNG-1.3.2-cp38-cp38-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ff8d3e9acad67b7d67c9d89cffd00f5303fd721dbd94c228c86db5b3b9e52c1 |
|
MD5 | b4997f011d9880261af134c2717745db |
|
BLAKE2b-256 | b2397c081711ea4bf69f89b0114ccfb0feb23c45da3578f30946ee72a516e555 |
Hashes for SyncRNG-1.3.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2279b5352a3f42fe937a911c8fafa8c26c79e16d0b0daeff5ec333755eefd2b |
|
MD5 | 090da954c7961fb7d3b77c6dcad1ac7e |
|
BLAKE2b-256 | 550e85ff711f14ffd484648f2cc8f8dc4df8d6d4f224c9083f48b86b9c762ff5 |
Hashes for SyncRNG-1.3.2-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a82e3136ec804f32689297d8cd93ad5fe51c60f09241e152f31d9d9e6c3cb6b1 |
|
MD5 | 2724987ba81c415b8e74881ad473020a |
|
BLAKE2b-256 | b273cd824bf0261a901d39ac42fbba934d56883a37f268f5070ae14cd2354f95 |
Hashes for SyncRNG-1.3.2-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d2bc0af4d68c75fd56347e4cef5441cb3eb14cc361f2219bfca25b2d61f80a8 |
|
MD5 | a9ea80c45e4ee8bbad20056d500225b5 |
|
BLAKE2b-256 | d9c0b19cb360363ce36cb6e478fa2d0a63ba68d21e1077e609565ca0540cb53b |
Hashes for SyncRNG-1.3.2-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11e506bcd1d7fceebe23420620123ec9b2ac6f9087964fade977687b9ba35e18 |
|
MD5 | b2fc1a84ffd2bbdd01c896b9c0f5becb |
|
BLAKE2b-256 | bef662f8b1c00a988ec0255e9f19cb67678439260280f8828996765e3a1a7a39 |
Hashes for SyncRNG-1.3.2-cp37-cp37m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd9fe4c43176c7bcbf03f1c7618a1068a3f9bc0ae86d6ca5d2567add9fc6628c |
|
MD5 | 6e85c7aeceb7eb215788361f05f428f8 |
|
BLAKE2b-256 | 553c3b8580b19d0cc99aec61a04fa60e0b172fe3b974f887bdffe4da62fb34e9 |
Hashes for SyncRNG-1.3.2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdcce5212bc8321c4af1fdbbcdf91d60683df63dba60722a3d96ceb96a9dcefe |
|
MD5 | 3717719d00a8517b4d1a418f28b41086 |
|
BLAKE2b-256 | 3feaf16feb24fcf1ca5d39b35eec74bd64eea1483c8dc9c0752843015f541a21 |
Hashes for SyncRNG-1.3.2-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ddfaeecbc3daf25c65216fb365157b9a11b279460be20dfccdacfee774039cea |
|
MD5 | e083af40ee55a24c191266f867bb9393 |
|
BLAKE2b-256 | 9e42e421a52f96a5058d63887047608d0b7d16721b3689bf85ed0c19756a99b9 |
Hashes for SyncRNG-1.3.2-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39532409094b79a38c6fefb86370acebcb949b6916aba45fff9360c25a410e46 |
|
MD5 | a41533bd3fca7cceb9fc37ce4ae0e47e |
|
BLAKE2b-256 | f9c9a0c989fc00cea35bd0e8aac5ac236cffc6b8acbcc3841afaade05470319a |
Hashes for SyncRNG-1.3.2-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f48c03c66e2d150195c4619d3661c1e8cd76809d81fe468e80c3a8d6ee4dc754 |
|
MD5 | 677b9aabb8bd830a745c05058ad06f7c |
|
BLAKE2b-256 | eb24a417c8416a96445b56dde999f8acea6a1e096199439e1ca2ca6ad494a735 |
Hashes for SyncRNG-1.3.2-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41979422953abfe20751fc4c3d638974445b869ec73ab797a1a0d4cbfd3ced1c |
|
MD5 | 7a15dc0ec3c16b422f389326ba547697 |
|
BLAKE2b-256 | c6881bd75d6af048db95c4c1c86beea76dda058da68b1ed26127a8801664acfb |
Hashes for SyncRNG-1.3.2-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8daa430442cdc9f139a4af6ce8656bfe0bfa4a2b65af0634c9766753577c145 |
|
MD5 | 00b728a1df99bab74370f7112e99708b |
|
BLAKE2b-256 | 475ffc5f7846739e053f81430501670309da7aa36fb9f9540dfe5de2ff509c0b |
Hashes for SyncRNG-1.3.2-cp36-cp36m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b3d1287eca6420448a6f3ebe4edc379238e4621151ec95208877e6fcf740329 |
|
MD5 | 53bcf08ae236fd0c2ef28d2db8df7f2d |
|
BLAKE2b-256 | 9b7bc0d4cedabdd2203181ec9a19debc697e3162fd6e4e96e31c824ffc4e54f7 |
Hashes for SyncRNG-1.3.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92c739d8151ab20fb8f0dcd01403c6f37f148b6c54fc9e97ee2e993799b81c9c |
|
MD5 | ff0c6e95160356184f5e97cf0d877641 |
|
BLAKE2b-256 | e728c030dac3844ac867ecdaf78e401a8d00ac36a9e68d57ef2f5fb419d06707 |
Hashes for SyncRNG-1.3.2-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6aba8ea9406f84a49ca4c3604e20aea87d81203f16ae6361caf55975bac2ed4a |
|
MD5 | a50c4502900c307ca8776f069ebb4206 |
|
BLAKE2b-256 | ce2383e2e7b5a7585e9f5df422506ac5816645bb0f82f858a229d810e36636c4 |
Hashes for SyncRNG-1.3.2-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 827850c45899d95abd983ef2f64babac8ae21af61eaa07f89015815d0b17195c |
|
MD5 | 3d39b70fa27a5f958c5e4c037c6e050a |
|
BLAKE2b-256 | 6d700d3d289eb129593f3016d422884fda05d48a300765b013254de628060a44 |