swh:1:snp:d1587d616651317fdcebcbb237dce82c32266449
Tip revision: 8624da9b79d1f0906386782ccf047a68cd05098d authored by Diethelm Wuertz on 08 August 1977, 00:00:00 UTC
version 221.10065
version 221.10065
Tip revision: 8624da9
015B-PortableInnovations.R
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
# Copyrights (C)
# for this R-port:
# 1999 - 2004, Diethelm Wuertz, GPL
# Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
# info@rmetrics.org
# www.rmetrics.org
# for the code accessed (or partly included) from other R-ports:
# see R's copyright and license files
# for the code accessed (or partly included) from contributed R-ports
# and other sources
# see Rmetrics's copyright file
# ##############################################################################
# FUNCTION: DESCRIPTION:
# set.lcgseed Set initial random seed
# get.lcgseed Get the current valus of the random seed
# runif.lcg Uniform linear congruational generator
# rnorm.lcg Normal linear congruational generator
# rt.lcg Student-t linear congruational generator
################################################################################
# DESCRIPTION:
# A Simple Portable Random Number Generator for Use in R and Splus
# Use this generator only for comparisons of Programs in R and Splus !!!
# Method: A linear congruential generator with
# LCG(a=13445, c=0, m=2^31-1, X0)
# Note, this is a random number generator which passes the bitwise
# randomness test.
# Reference:
# http://csep1.phy.ornl.gov/rn/node13.html
# N. S. Altman. ``Bitwise Behavior of Random Number Generators,''
# SIAM J. Sci. Stat. Comput., 9(5), September, pps. 941-949, 1988
# Example:
# set.lcgseed(4711)
# cbind(runif.lcg(100), rnorm.lcg(100), rt.lcg(100, df=4))
################################################################################
set.lcgseed =
function(seed = 4711)
{ # A function implemented by Diethelm Wuertz
# Description:
# Sets the random seed for the linear congruential
# random number generator
# FUNCTION
# Return Value:
lcg.seed <<- seed
}
# ------------------------------------------------------------------------------
get.lcgseed =
function()
{ # A function implemented by Diethelm Wuertz
# Description:
# Returns the random seed for the linear congruential
# random number generator
# FUNCTION
# Return Value:
lcg.seed
}
# ------------------------------------------------------------------------------
runif.lcg =
function(n, min = 0, max = 1)
{ # A function implemented by Diethelm Wuertz
# Description:
# A linear congruential generator for uniform distributed
# random numbers
# Notes:
# Important - Use this generator only for comparisons of
# Programs in R and SPlus (and not for production) !!!
# Portable Random Numbers:
# A linear congruential generator
# LCG(a=13445, c=0, m=2^31-1, X0)
# This is a random number generator which
# passes the bitwise randomness test
# References:
# http://csep1.phy.ornl.gov/rn/node13.html
# N. S. Altman. ``Bitwise Behavior of Random Number Generators,''
# SIAM J. Sci. Stat. Comput., 9(5), September, pps. 941-949, 1988
# FUNCTION
# Initialize:
if(!exists("lcg.seed")) lcg.seed <<- 4711
# Generate:
r.lcg = rep(0, times = n)
a = 13445
c = 0
m = 2^31-1
for (i in 1:n) {
lcg.seed <<- (a*lcg.seed + c) %% m
r.lcg[i] = lcg.seed/m }
r.lcg = (max-min)*r.lcg + min
# Return Value:
r.lcg
}
# ------------------------------------------------------------------------------
rnorm.lcg =
function(n, mean = 0, sd = 1)
{ # A function implemented by Diethelm Wuertz
# Description:
# A linear congruential generator for normal distributed
# random numbers
# FUNCTION
# This is slow, but portable between R and SPlus
(qnorm(runif.lcg(n = n, min = 0, max = 1)) - mean)/sd^2
}
# ------------------------------------------------------------------------------
rt.lcg =
function(n, df)
{ # A function implemented by Diethelm Wuertz
# Description:
# A linear congruential generator for Sudent-t distributed
# random numbers
# FUNCTION
# This is slow, but portable between R and SPlus
qt(runif.lcg(n = n, min = 0, max = 1), df = df)
}
################################################################################