atUsesRandom
Encapsulate Use of Random Numbers in Tests

### Description

Run an expression which generates random numbers and restore the value of .Random.seed to what it was before running the expression. Thus assertionTest() will not complain that the test uses random numbers.

### Usage

    atUsesRandom(expr)

### Arguments

expr | An unevaluated expression. The expression will be taken literally, as with system.time().

### Details

The current value of .Random.seed, if any, will be saved, then the expression will be evaluated by lazy evaluation, and finally the state of .Random.seed will be restored to its original value. assertionTest() will complain if an expression alters .Random.seed and atUsesRandom is the recommended way to prevent this complaint. If you generate test data using a random number generator it is best to generate all such data for a test in one call to atUsesRandom(), since atUsesRandom(x<-runif(5)) followed by atUsesRandom(y<-runif(5)) may result in x and y being identical.
atUsesRandom will print a message if evaluating the expression does not alter .Random.seed.
Note that if .Random.seed exists before atUsesRandom(expr) is called, then it will be restored to its prior value so that a second call to atUsesRandom(expr) will generate the same random numbers. However, if .Random.seed does not exist prior to the call, it won't exist after the call either and the second call will use an independent stream of random numbers. (We may change this in the future so that atUsesRandom will always or never leave a .Random.seed behind so that it always gives repeatable results.)
Value
The value of the evaluated expression.
See Also
assertionTest
Examples

    ## Not run:
    # Put the following in a test file, test.t,
    # and run it through assertionTest.
    {atUsesRandom({
        x <- runif(5)
        y <- runif(5)})
        atOnExit(rm(x,y))
    }
    atExpectStop(lm(y ~ poly(x,5)), expected="degree.*must be less")
    # Run example test files in assertionTest/test-ex
    assertionTest(system.file(package="assertionTest","test-ex","mixed.t"))

    ## End(Not run)

Package assertionTest version 6.1.5-18
Package Index