code: private static double[] _ttbdInner (double target, double[] lower, double[] upper, RandomGenerator randomGenerator) {
          // Initialize the return value:
          final double[] retval = new double[lower.length];
  
          // Iterate over the retval and simulate:
          for (int i = 0; i < retval.length; i++) {
              if (lower[i] == upper[i]) {
                  retval[i] = lower[i];
              }
              else {
                  retval[i] = new UniformRealDistribution(randomGenerator, lower[i], upper[i]).sample();
              }
          }
  
          // Compute the gap of simulated total and target total:
          double gap = target - DMatrixUtils.sum(retval);
  
          // Iterate over the return values and adjust as per gap:
          for (int i = 0; i < retval.length; i++) {
              // If there is no gap, return the retval:
              if (gap == 0.0) {
                  return retval;
              }
  
              // Calculate the distances to limits:
              final double distanceToLower = lower[i] - retval[i];
              final double distanceToUpper = upper[i] - retval[i];
  
              // Compute the permissible shift:
              final double shift = gap > 0 ? Math.min(distanceToUpper, gap) : Math.max(distanceToLower, gap);
  
              // Apply the shift:
              retval[i] += shift;
  
              // Update gap:
              gap -= shift;
          }
  
          // Done, return:
          return retval;
      }
docs: Returns a target-total bounded distribution sample.
  
  @param target The target total value.
  @param lower Lower limits.
  @param upper Upper limits.
  @param randomGenerator The random generator.
  @return A vector of target-total bounded sample.