Home > Software engineering >  Random Uniform Numbers WITHOUT runif()
Random Uniform Numbers WITHOUT runif()

Time:05-29

Usually to generate a sequence of uniform random numbers, I use the "runif()" command in R:

runif(10,0,10)

 [1] 5.032995 8.712604 4.400579 3.874882 2.401324 2.465861 2.59525 8.570266 2.729831 5.176705

Out of curiosity, I was wondering if there might be some other way to generate random uniform numbers WITHOUT using the "runif" command. I have heard that random numbers can be generated using the computer's clock, but I am not sure how this is done.

  • Is it possible to write an R script that generates random uniform numbers (between some range) such that the "runif" command is not used?

Thank you!

@ Dmitry's Answer in a Loop:

v = rep(0,100)
v[1]=3
for (j in 1:100) {
v[j 1]=(65539*v[j])%%(2^31)
}

    i = 1:101

range01 <- function(x, ...){(x - min(x, ...)) / (max(x, ...) - min(x, ...))}
      
    rand_data = data.frame(i,v)
    rand_data$int_version = range01(rand_data$v)


plot(rand_data$v, type = "b", main = "100 Random Real Numbers with Randu")

plot(rand_data$int_version, type = "b", main = "100 Random Integers with Randu")

enter image description here enter image description here

CodePudding user response:

You could acquire the fractional seconds from Sys.time and use it as a poor-man's hack for a single draw:

as.numeric( substr( as.character( unclass(Sys.time())), start=11,stop=16))

It's not going to be a particularly good method of generating a sequence of random numbers.

CodePudding user response:

Yes, of course, you'd have to implement one of the pseudo-random number generation algorithms yourself. Start with RANDU, but never use it for practical purposes, ha-ha

  • Related