Generate integer random numbers from range (0:10^12)

后端 未结 5 1416
予麋鹿
予麋鹿 2021-01-07 17:54

I want to generate 10000 integer random numbers between 0 and 10^12. Usually, the code would look like this:

x <- sample(0:1000000000000,10000,replace=T)
         


        
5条回答
  •  太阳男子
    2021-01-07 18:20

    as.integer(runif(10000, min = 0, max = (1 + 10^12)))
    

    FYI: as.integer performs a truncation, not a rounding.

    In order to test if it works you can try by generating numbers in a smaller interval (i.e. from 0 to 6) and visualize the histogram of the result to see if the result is a uniform distribution, i.e.

    test <- as.integer(runif(10000, min = 0, max = (6 + 1)))
    hist(test)
    

提交回复
热议问题