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

后端 未结 5 1518
予麋鹿
予麋鹿 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:35

    The package extraDistr provides a range of additional probability distributions to sample from, including a discrete uniform distribution.

    Random sampling with function rdunif works like other stats random sampling functions included with R like runif, and avoids needing to round as in other solutions:

    > library("extraDistr")
    > rdunif(n = 10000, min = 0, max = 10^12)
    [1] 699559531175 881392957410 315869810758 941600866616
    [5] 906084092567 681591022527 514061764115 122652820777
    [9] 583204373950 517842726316 741211620393 422150962055 ...
    

提交回复
热议问题