Here,
I am trying to store the number of iterations and random numbers for a given number(total).
In this code, I used n=10 for taking the random numbers(positive) of 50 till it reaches <=3 (the differences).
I want to store the value upto the iteration that fulfills the condition as well as the difference of the last step.
n=10
m=matrix(NA,nrow=n,ncol=2)
total=50
for (i in 1:n){
a <- runif(1, 0, total)
m[i,1]=i
m[i,2]=a
total_new=total-a
total=total_new
if (total_new <= 3){
break
}
}
m
Expected outcome:
Here, set.seed is just for the easier view of what I am expecting.
set.seed(100)
n=10
m=matrix(NA,nrow=n,ncol=2)
total=50
for (i in 1:n){
a <- runif(1, 0, total)
m[i,1]=i
m[i,2]=a
total_new=total-a
total=total_new
if (total_new <= 3){
break
}
}
m[7,]=c(7,50-sum(m[1:6,2]))
m[1:7,]