Home > Software engineering >  'Random' sample with underlying pattern
'Random' sample with underlying pattern

Time:04-06

I want to create a vector of random integers with values between 1 and 10, with a gradual increase in the likelihood of higher numbers appearing as I progress through the vector. But, I still want the values to be mixed together, i.e., I'm trying to avoid 1,1,1,1,2,2,2,3... etc

I have so far managed the following, which works, but surely there is a more elegant way of achieving this?

# Generate 3 vectors of 1000 random integers, each with slightly different distributions
LOW <- rpois(1000, 2)
MID <- rpois(1000, 4)
HIGH <- rpois(1000, 7)

# Remove any values outside the desired range of 1:10
LOW <- LOW[LOW>0 & LOW<11]
MID <- MID[MID>0 & MID<11]
HIGH <- HIGH[HIGH>0 & HIGH<11]

# Sample 200 values from each vector
LOW <- sample(LOW, 200)
MID <- sample(MID, 200)
HIGH <- sample(HIGH, 200)

# Combine into a single vector
COMBINED <- c(LOW, MID, HIGH)

# Check distribution
X <- 1:600
plot(COMBINED)
lines(predict(lm(COMBINED~X)), col='red', lwd = 6)

enter image description here

CodePudding user response:

You don't say anything about the distribution you actually want, but if a binomial distribution is acceptable:

plot(1 rbinom(n=600, size=9, prob = seq(0.1,0.9,l=600)))

enter image description here

CodePudding user response:

You could use increasing lambda value (assuming a poisson distribution):

f <- function(n, k) rpois(n, rep(seq(n/k), each = k))

plot(f(100, 10))

enter image description here

  •  Tags:  
  • r
  • Related