dimanche 16 août 2020

How can I create unique random numbers in R?

I hope to generate random numbers between 1:100 and then test their divisibility by 3. I have created a loop function.

v <- c(0)
for(i in 1:100){
  r <- floor(runif(1, min=1, max=100))
  if(r %% 3 == 0){
    v <- append(v,r)
  }
}
print(v)

However, the numbers do keep repeating as you can see in the following output. Is there any way to only generate unique multiples of 3 between 1:100. I am aware there's a way to use the seq function and generate the same numbers, but I still want to know how to acquire unique random numbers.

Output: [1] 0 18 87 30 45 90 12 72 75 60 27 84 90 27 42 54 63 15 63 30 72 69 57 30 3 6 15 30 3 [30] 60 72 6 6 18 75 96 84 78 24




Aucun commentaire:

Enregistrer un commentaire