Skip to main content

A synchronized Tausworthe RNG for Python and R

Project description

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 Python you can do:

from SyncRNG import SyncRNG

s = SyncRNG(seed=123456)
for i in range(10):
  print(s.randi())

Similarly, after installing the R library you can do in 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 CRAN:

install.packages('SyncRNG')

The Python package can be installed using pip:

pip install syncrng

Usage

In both R and Python the following methods are available for the SyncRNG class:

  1. randi(): generate a random integer on the interval [0, 2^32).

  2. rand(): generate a random floating point number on the interval [0.0, 1.0)

  3. randbelow(n): generate a random integer below a given integer n.

  4. shuffle(x): generate a permutation of a given list of numbers x.

Notes

The random numbers are uniformly distributed on [0, 2^32 - 1].

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

SyncRNG-1.2.1.tar.gz (5.0 kB view details)

Uploaded Source

File details

Details for the file SyncRNG-1.2.1.tar.gz.

File metadata

  • Download URL: SyncRNG-1.2.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for SyncRNG-1.2.1.tar.gz
Algorithm Hash digest
SHA256 65cbe9826bcd819f06e32b49812471805d961aa6edd9d523db0a077c8aaa6513
MD5 a5fe13516425b869ce8ad87cd19a9f55
BLAKE2b-256 390cb1446dd7c9b6c2769d04a27e8a375387558cb6beb4d503f143b9b89d2b44

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page