简体   繁体   English

没有 runif() 的随机统一数

[英]Random Uniform Numbers WITHOUT runif()

Usually to generate a sequence of uniform random numbers, I use the "runif()" command in R:通常为了生成一系列均匀随机数,我使用 R 中的“runif()”命令:

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.出于好奇,我想知道是否有其他方法可以在不使用“runif”命令的情况下生成随机统一数。 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?是否可以编写一个生成随机统一数字(在某个范围之间)的 R 脚本,以便不使用“runif”命令?

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")

在此处输入图像描述 在此处输入图像描述

You could acquire the fractional seconds from Sys.time and use it as a poor-man's hack for a single draw:您可以从 Sys.time 获取小数秒,并将其用作单次平局的穷人黑客:

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.这不会是生成随机数序列的特别好的方法。

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-haRANDU开始,但千万不要用在实际目的上,哈哈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM