使用hitandrun包R生成随机数
我试图用R的hitandrun包生成均匀随机数。我使用不等式和等式约束。这是我的代码:使用hitandrun包R生成随机数,r,random,R,Random,我试图用R的hitandrun包生成均匀随机数。我使用不等式和等式约束。这是我的代码: library(hitandrun) ineq.constr<-mergeConstraints(lowerBoundConstraint(16,1,0),upperBoundConstraint(16,1,1)) for(i in 2:16){ ineq.constr<-mergeConstraints(ineq.constr, lowerBoundConstraint(16,i,0),upp
library(hitandrun)
ineq.constr<-mergeConstraints(lowerBoundConstraint(16,1,0),upperBoundConstraint(16,1,1))
for(i in 2:16){
ineq.constr<-mergeConstraints(ineq.constr,
lowerBoundConstraint(16,i,0),upperBoundConstraint(16,i,1))
}
add.constr<- mergeConstraints(
list(constr= c(1,1,1,-1,-1,-1,-1,1,-1,-1,-1,1,1,1,1,-1), dir='<=', rhs=4),
list(constr= c(-1,-1,-1,1,1,1,1,-1,1,1,1,-1,-1,-1,-1,1), dir='<=', rhs=-2))
ineq.constr<-mergeConstraints(ineq.constr,add.constr)
eq.constr<- mergeConstraints(
list(constr= c(1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0), dir='=', rhs=1),
list(constr= c(0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0), dir='=', rhs=1),
list(constr= c(0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0), dir='=', rhs=1),
list(constr= c(0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1), dir='=', rhs=1),
list(constr= c(1,-1,0,0,1,-1,0,0,0,0,0,0,0,0,0,0), dir='=', rhsh=0),
list(constr= c(0,0,1,-1,0,0,1,-1,0,0,0,0,0,0,0,0), dir='=', rhsh=0),
list(constr= c(0,0,0,0,0,0,0,0,1,-1,0,0,1,-1,0,0), dir='=', rhsh=0),
list(constr= c(0,0,0,0,0,0,0,0,0,0,1,-1,0,0,1,-1), dir='=', rhsh=0),
list(constr= c(1,0,-1,0,0,0,0,0,1,0,-1,0,0,0,0,0), dir='=', rhsh=0),
list(constr= c(0,1,0,-1,0,0,0,0,0,1,0,-1,0,0,0,0), dir='=', rhsh=0),
list(constr= c(0,0,0,0,1,0,-1,0,0,0,0,0,1,0,-1,0), dir='=', rhsh=0),
list(constr= c(0,0,0,0,0,1,0,-1,0,0,0,0,0,1,0,-1), dir='=', rhsh=0))
constr<-mergeConstraints(ineq.constr, eq.constr)
samples <- hitandrun(constr, n.samples=2000000)
stopifnot(dim(samples) == c(2000000, 16))
df<-matrix(unlist(samples), nrow=2000000, ncol=16)
df<-data.frame(df)
write.table(df, file="har_samples.txt", row.names=FALSE, col.names=FALSE)
库(hitandrun)
工程量清单
add.constr<- mergeConstraints(
list(constr= c(1,1,1,-1,-1,-1,-1,1,-1,-1,-1,1,1,1,1,-1), dir='<=', rhs=4),
list(constr= c(-1,-1,-1,1,1,1,1,-1,1,1,1,-1,-1,-1,-1,1), dir='<=', rhs=-2))
ineq.constr<-mergeConstraints(ineq.constr,add.constr)