everyone!
how to generate a vector which satisfy some conditions?
Problem: generate a vector a
such that length(a)=400000
which is made up of 8 elements:0, 5, 10, 50, 500, 5000, 50000, 300000
. Each element appears a set number of times, namely 290205, 100000, 8000, 1600, 160, 32, 2, 1
, respectively. Further, a
is blocked into 4,000 "groups" of 100 consecutive elements; call them a_k, k=1,...,4000
. These groups must satisfy the following:
- The sum of every group exceeds 150, i.e.
sum_i a_k_i>150
for allk
. - The elements
5
,10
and50
appear between 25 and 29 times in each group, i.e. for allk
, the set{i|a_i_k in (5,10,50)}
has magnitude between 25 and 29. 0
never appears more than 8 times in a row in any group.
I have tried this many times, but it does not seem to work: My current code is as follows:
T <- 4*10^(5) # data size
x <- c(0, 5, 10, 50, 500, 5000, 50000, 300000) #seed vector
t <- c(290205, 100000, 8000, 1600, 160, 32, 2, 1) #frequency
A <- matrix(0, 4000, 100) #4000 groups
k <- rep(0, times = 8) #record the number of seeds
for(m in 1:4000) {
p <- (t - k)/(T - 100*(m - 1)) #seed probability
A[, m] <- sample(x, 100, replace = TRUE, prob = p) #group m
sm <- 0
i <- 0
for(j in 1:92) {
if(sum(A[m,j:j + 8])==0){
if(A[m,j] > 0 & A[m,j] < 500) {i <- i+1}
sm <- sm+A[100*m+j]
}
else j <- 0
}
if (sm >= 150 & i > 24 & i < 30 & j != 0) {
m <- m + 1
for (n in seq_len(x)) {
k[n] <- sum(A[, m+1] == x[n]) + k[n]
}
}
}
I can start it off and maybe someone can help get to the next step. My approach is to start with the constraints and let
sample
work out the numbers.So this gets you a list of 4,000 groups of 100 numbers that fit the constraints. It only took about two seconds of system time.
Next step is for someone to get a way to build something similar except eliminate 300000 once it is used, and 50000 once it is used twice and so on.