I am using R
in RStudio
and I have the following existing R
codes, which give me 3 random
numbers as output ( 2 odd
and 1 even
).
foo <- list(
c(1,2,3,5,6,7,8,9),
c(11,13,15,16,17,19),
c(21,25,28,29),
c(31,33,34,35,37,38,40)
)
names(foo) <- c(2, 1, 0, 0)
is.odd <- function(x) x %% 2 != 0
countOdds<-function(dt){ sum(sapply(dt, is.odd)) }
while(T){
list2<- unlist(sapply(seq_along(foo), function(x) sample(foo[[x]], names(foo[x]))))
if(countOdds(list2)==2)
break
}
sort(list2)
Currently, in my existing codes, I can specify the following:
- list of numbers for each 4 lists shown above
- the number of items to be taken from each list
- the number of ODD/EVEN combination I need.
I want to add the following additional conditions to my existing R
codes:
- be able to specify the value of the
SUM
of the 3 numbers that needs to be output (that is, specify, say, 24, and the 3 random numbers must sum to 24) - output all the combination of 3 numbers that will SUM to 24 (or any other number I may input). Currently, it is only giving me 1 output of 3 random numbers.
Can these 2 conditions be added to my existing codes?
Aucun commentaire:
Enregistrer un commentaire