Lorcan Treanor
Lorcan Treanor

Reputation: 561

Using self-defined functions in R to produce different random numbers Each time


I am trying to produce different uniform numbers using the Lehmar random number generator. I believe I have done this but I have a problem in producing different numbers each time I execute this function. Below is the code I am trying to do and I will explain the problem further underneath it.

            MODULUS <- 2147483647
            MULTIPLIER <- 48271

            put_Seed <- function(x)
            {
              x <- (if ( x > 0)
              {
                x%%MODULUS
              }
              else
              {
                1000*as.numeric(Sys.time())
              }
              )
            }

            T_val <- function(Rand)
            {
              Q <- floor(MODULUS / MULTIPLIER)
              R <- MODULUS%%MULTIPLIER;  

              floor(MULTIPLIER*(Rand%%Q) - R*(Rand/Q))
            }  

            New_Random_Seed <- function(T_value_i)
            {
              Random_Seed <- (if (T_value_i > 0)
              {
                T_value_i;
              }
              else
              {
                T_value_i + MODULUS
              })
            }

            Random <- function(New_Seed)
            {
              New_Seed/MODULUS
            }

            uniform_num <- function(a, b, r)
            {
              a + (b - a) * r
            }

            Random_Seed <- put_Seed(123456789)

            uni_num <- function(k)
            {

              Random_Seed <- put_Seed(k)
              T_value <- T_val(Random_Seed)
              Random_Seed <- New_Random_Seed(T_value)
              uniform_num(0, 1, Random(Random_Seed))

            }

            test1 <- uni_num(Random_Seed)
            test2 <- uni_num(Random_Seed)
            test3 <- uni_num(Random_Seed)

#Results
    #test1 = 0.05380306
    #test2 = 0.05380306
    #test3 = 0.05380306


What I am trying to do is whenever I run the uni_num function that each time, the Random_Seed gets updated and the uniform_num(0, 1, Random(Random_Seed)) line produces a random uniform number between 0 and 1 each time the function is executed. The code works for 1 repetition but if I try to use the function again the Random_Seed has not being updated and hence the function will produce the same random number as before. This is undesirable as I wish to produce different random number's each time by having the Random_Seed updated after each repetition. Forgive me if there is a simple solution but my head is wrecked from trying to find an answer. Cheers :)

Upvotes: 0

Views: 119

Answers (1)

Matthew Lundberg
Matthew Lundberg

Reputation: 42689

In this function:

        uni_num <- function(k)
        {

          Random_Seed <- put_Seed(k)
          T_value <- T_val(Random_Seed)
          Random_Seed <- New_Random_Seed(T_value)
          uniform_num(0, 1, Random(Random_Seed))

        }

The target of the assignment Random_Seed <- is in the environment of the function body, and not the global environment. Thus, when you make a second call, Random_Seed in the global environment has not been modified, and you get the same results.

To write to the global environment instead, use <<-:

Random_Seed <<- New_Random_Seed(T_value)

It looks like this is the only assignment that must be modified, as New_Random_Seed returns a value and doesn't require modification of this global object. In addition, the first assignment to Random_Seed in uni_num might as well be in the function body environment.

Note that it's bad form for a function to write to the global environment. There's always a better way. But this will work for your example code.

Upvotes: 1

Related Questions