Случайные числа

Простые типы / Числа / Integer

# Both ends included
rand <- sample(0:2, 1)
# rand is 0, 1 or 2

# A single number n means 1:n
rand2 <- sample(3, 1)
# rand2 is 1, 2 or 3

# A random element
rand3 <- sample(c("red", "green", "blue"), 1)
# rand3 is one of the colors

# Trap: one element left, and it means 1:n
candidates <- c(3, 7, 9)
big <- candidates[candidates > 8]
pick <- sample(big, 1)
# pick is from 1 to 9, not always 9

cat("rand =", rand, "\n")
cat("rand2 =", rand2, "\n")
cat("rand3 =", rand3, "\n")
cat("pick =", pick, "\n")