1

I need to generate random numbers until the condition(sum of the difference of each random number is 3000.) is satisfied.

and random numbers are already generated from the truncated normal distribution.

I already try to use while loop while(1) and if , but it didn't work properly.

How can I generate new random number sequence with the condition?

truncnorm_rn <- rtruncnorm(1000,0,30,0,1)

a <- 0
b <- 0
c <- 0 # sum of diffrences between truncnorm random number 
d <- NULL # New sequnce with condition

while(1){
         a <- truncnorm_rn[1]
         b <- truncnorm_rn[2]
         c <- sum(a - b)
         if(c > 3000) break
}
Brad Yun
  • 13
  • 3
  • 3
    Can you provide the code for the while loop that you've already tried? Have you tried using the `truncnorm` package for generating the random numbers? – Harrison Jones Oct 06 '21 at 12:53
  • I've already used 'truncnorm' and generating random numbers that follow truncated normal distribution. however, I need to generate new sequence of random numbers until sum of the difference of each random number is 3000. the code I will be posted together, but it didn't work. – Brad Yun Oct 06 '21 at 13:06
  • So to clarify, you want to generate a *new* set of 1,000 truncated normal numbers each iteration of the while loop and see if the absolute difference between all those numbers is greater than 3000. If the absolute difference is greater than 3000, stop the while loop? – Harrison Jones Oct 06 '21 at 13:26
  • each iteration of the while loop is just comparing the first two random numbers, so you'd need to increment a counter. Alternatively you could ditch the loop with something like `head(which(diff(truncnorm_rn)>3000),1)` – Miff Oct 06 '21 at 13:40
  • @HarrisonJones thank you for your kind question. yes, I want to quit the loop before sum of the differences is over 3000. and want to generate sequence with that condition. – Brad Yun Oct 06 '21 at 14:34
  • @Miff Thank for your help I will try this one for solving this problem. – Brad Yun Oct 06 '21 at 14:35

1 Answers1

0

What you're asking for is the following. Which will generate a new truncated normal series of size 1,000 until there is an absolute difference greater than 3,000 for all numbers in the series.

library(truncnorm)

abs_diff <- 0
i <- 0

while(abs_diff <= 3000) {
  
  i <- i + 1
  series <- rtruncnorm(n = 1000, a = 0, b = 30, mean = 0, sd = 1)
  abs_diff <- sum(abs(diff(series)))
  
}

# the iteration where the sum of absolute differences is > 3,000
i
# the series of numbers that satisfied the condition
series

However, I don't know if your condition will be satisfied with those parameters for the truncated normal (quickly, at least). I ran this for 125k iterations and the condition was not satisfied. If you change the mean or sd (see example code below) you'll find a series that satisfies the condition in a more reasonable amount of time.

abs_diff <- 0
i <- 0

while(abs_diff <= 3000) {
  
  i <- i + 1
  series <- rtruncnorm(n = 1000, a = 0, b = 30, mean = 5, sd = 2.6)
  abs_diff <- sum(abs(diff(series)))
  
}
Harrison Jones
  • 2,256
  • 5
  • 27
  • 34
  • Thank you for your sensitive answer. I try again with changing truncated normal mean and sigma and then easily find random number sequence for simulation. Thank you :) – Brad Yun Oct 06 '21 at 19:54