In: Statistics and Probability
We will use R studio to generate 30 random numbers from 1 to 500.
For uniformly distributed (flat) random numbers, use
runif()
.
> # Get a vector of 30 numbers from 1 to
500
> runif(30, min=1, max=500)
[1] 222.99233 388.80694 420.16994 432.68283 105.26675 69.09798
185.37575 320.34343 359.98758 212.93607
[11] 480.50060 151.20446 363.15961 409.10138 396.90751 287.54401
474.05881 202.05784 354.77375 166.57480
[21] 44.84848 11.94746 479.14588 241.92003 207.10843 260.75617
367.05484 78.71087 123.79068 172.80188
> # Get 30 integers from 1 to 500
> # Use max=501 because it will never actually equal
501
> floor(runif(30, min=1, max=501))
[1] 286 123 255 123 466 233 101 96 31 407 340 422 373 419 491 344
161 284 62 187 210 449 159 204 395
[26] 424 238 439 131 500
> # This will do the same thing
> sample(1:500, 30, replace=TRUE)
[1] 143 346 106 317 433 296 490 157 50 346 2 354 206 121 127 491
437 432 214 285 397 248 490 317 310
[26] 334 94 495 366 38
> # To generate integers WITHOUT
replacement:
> sample(1:500, 30, replace=FALSE)
[1] 196 169 442 38 365 104 123 357 244 3 277 128 214 426 184 272
327 293 281 59 421 9 236 71 60
[26] 12 221 358 25 260
We can use the above methods to generate random numbers.