rsimpl=function (n = 1, m, s) {

  #from rmutil by Swihart & Lindsey (2022) but with minor adjustment
  p=stats::runif(n)
  if(m>.99) m=.99
  if(m<.01) m=.01
  if(s<.05) s=.05
  if(p>.999) p=.999
  if(p<(1-.999)) p=(1-.999)

  h <- function(y) .C("psimplex_c", as.double(y), as.double(m[i]),
                      as.double(s[i]), as.double(1), len = as.integer(1), eps = as.double(1e-06),
                      pts = as.integer(5), max = as.integer(16), err = integer(1),
                      res = double(1))$res - p[i]  # C function is adapted from R package rmutils, see the source code for copyright
  len <- max(length(p), length(m), length(s))
  if (length(p) != len) {
    if (length(p) == 1)
      p <- rep(p, len)
    else stop("length of p incorrect")
  }
  if (length(m) != len) {
    if (length(m) == 1)
      m <- rep(m, len)
    else stop("length of m incorrect")
  }
  if (length(s) != len) {
    if (length(s) == 1)
      s <- rep(s, len)
    else stop("length of s incorrect")
  }
  tmp <- vector(mode = "numeric", len)
  for (i in 1:len) {
    interval <- c(1e-06, 0.999999)
    h1=h(interval[1])
    h2=h(interval[2])
    while ((h1*h2 > 0)){
      interval <- c(interval[1]/10, 1 - interval[1]/10)
      h1=h(interval[1])
      h2=h(interval[2])
    }
    if((h1*h2 < 0)) tmp[i] <- stats::uniroot(h, interval)$root
    else{
      tmp[i]=NA
      warning("NA's produced is simulating simplex IRT responses - stats::uniroot() failed")
    }
  }
  return(tmp)
}
