diff options
| -rw-r--r-- | DESCRIPTION | 11 | ||||
| -rw-r--r-- | NAMESPACE | 3 | ||||
| -rw-r--r-- | Python/SyncRNG.py (renamed from SyncRNG.py) | 0 | ||||
| -rw-r--r-- | R/SyncRNG.R (renamed from SyncRNG.R) | 30 | ||||
| -rw-r--r-- | man/SyncRNG-class.Rd | 37 | ||||
| -rw-r--r-- | setup.py | 5 | ||||
| -rw-r--r-- | src/syncrng.c (renamed from syncrng.c) | 0 | ||||
| -rw-r--r-- | test/test.R (renamed from test.R) | 0 | ||||
| -rw-r--r-- | test/test.py (renamed from test.py) | 0 |
9 files changed, 72 insertions, 14 deletions
diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..6620df7 --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,11 @@ +Package: SyncRNG +Version: 0.1 +Date: 2015-07-31 +Title: A Synchronized Tausworthe RNG for R and Python +Author: Gertjan van den Burg <gertjanvandenburg@gmail.com> +Maintainer: Gertjan van den Burg <gertjanvandenburg@gmail.com> +Depends: R (>= 3.0.0) +Description: Random number generation designed for cross-language usage. +License: file LICENSE +Imports: + methods diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000..bb2ff1a --- /dev/null +++ b/NAMESPACE @@ -0,0 +1,3 @@ +useDynLib(SyncRNG) +export(SyncRNG) +import(methods) diff --git a/SyncRNG.py b/Python/SyncRNG.py index 7ef6fd6..7ef6fd6 100644 --- a/SyncRNG.py +++ b/Python/SyncRNG.py @@ -1,22 +1,24 @@ library(methods) -frame.files <- lapply(sys.frames(), function(x) x$ofile) -frame.files <- Filter(Negate(is.null), frame.files) - -script.dir <- normalizePath(dirname(frame.files[[length(frame.files)]])) -source.file <- paste(script.dir, '/', 'RSyncRNG.so', sep='') - -dyn.load(source.file) - +#' A Reference Class for SyncRNG +#' +#' @field seed The seed for the random number generator +#' @field state The current state of the RNG, should not be modified by the +#' user +#' +#' @examples +#' s = SyncRNG(seed=123456) +#' for (i in 1:10) +#' cat(s$randi(), '\n') +#' SyncRNG <- setRefClass('SyncRNG', fields=list( seed='numeric', - state='numeric', - BPF='numeric' + state='numeric' ), methods=list( initialize=function(..., seed=0) { - BPF <<- 32 + "Initialize the RNG using the C function R_syncrng_seed" seed <<- seed tmp <- .Call('R_syncrng_seed', as.numeric(seed)) @@ -24,17 +26,20 @@ SyncRNG <- setRefClass('SyncRNG', callSuper(...) }, randi=function() { + "Generate a single random 32-bit integer" tmp <- .Call('R_syncrng_rand', as.numeric(state)) state <<- tmp[1:4] return(tmp[5]) }, rand=function() { + "Generate a single random float in the range [0, 1)" r <- randi() return (r * 2.3283064365387e-10) }, randbelow=function(n) { - maxsize <- 2^BPF + "Generate a random integer below a given number" + maxsize <- 2^32 if (n >= maxsize) { warning(paste("Underlying random generator ", "does not supply\n enough bits ", @@ -50,6 +55,7 @@ SyncRNG <- setRefClass('SyncRNG', return(round(r*maxsize) %% n) }, shuffle=function(x) { + "Randomly shuffle a provided array of values" y <- x for (i in rev(1:(length(y)-1))) { j <- randbelow(i+1) diff --git a/man/SyncRNG-class.Rd b/man/SyncRNG-class.Rd new file mode 100644 index 0000000..599b540 --- /dev/null +++ b/man/SyncRNG-class.Rd @@ -0,0 +1,37 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/SyncRNG.R +\docType{class} +\name{SyncRNG-class} +\alias{SyncRNG} +\alias{SyncRNG-class} +\title{A Reference Class for SyncRNG} +\description{ +A Reference Class for SyncRNG +} +\section{Fields}{ + +\describe{ +\item{\code{seed}}{The seed for the random number generator} + +\item{\code{state}}{The current state of the RNG, should not be modified by the +user} +}} +\section{Methods}{ + +\describe{ +\item{\code{initialize(..., seed = 0)}}{Initialize the RNG using the C function R_syncrng_seed} + +\item{\code{rand()}}{Generate a single random float in the range [0, 1)} + +\item{\code{randbelow(n)}}{Generate a random integer below a given number} + +\item{\code{randi()}}{Generate a single random 32-bit integer} + +\item{\code{shuffle(x)}}{Randomly shuffle a provided array of values} +}} +\examples{ +s = SyncRNG(seed=123456) +for (i in 1:10) + cat(s$randi(), '\\n') +} + @@ -7,12 +7,13 @@ setup( version='0.1', description='A synchronized Tausworthe RNG for Python and R', license='GPL v2', - py_modules=['SyncRNG'], + package_dir={'': 'Python'}, + packages=[''], ext_modules=[ Extension( "syncrng", define_macros=[('TARGETPYTHON', '1')], - sources=["syncrng.c"] + sources=["src/syncrng.c"] ) ], ) diff --git a/syncrng.c b/src/syncrng.c index 668c39e..668c39e 100644 --- a/syncrng.c +++ b/src/syncrng.c |
