0

How can I do this? I know how to generate random numbers but not in a fixed range.

Barrenr
  • 11
  • 3
  • see this post https://stackoverflow.com/questions/22380890/generate-n-random-numbers-whose-sum-is-m-and-all-numbers-should-be-greater-than – Umair Mubeen Jul 27 '20 at 15:39
  • if you want the range it will be `rand.nextInt(Math.abs(x-y)) + Math.min(x, y)` – locus2k Jul 27 '20 at 15:49
  • If you have a new question, you should ask it as a separate question instead of editing this one. For this specific question, this would also be a duplicate, however - see https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java. Or if you need doubles instead of integers, see https://stackoverflow.com/questions/3680637/generate-a-random-double-in-a-range – Hulk Jul 30 '20 at 16:47

4 Answers4

2

why dont you just simply random the sum. then generate n random numbers with that sum

public static void random(int n, int min, int max) {
    Random random = new Random();
    int sum = random.nextInt(max - min) + min;
    List<Integer> list = new ArrayList<>();
    int currentSum = 0;
    for (int i = 0; i < n - 1; i++) {
        int value = random.nextInt((int) (sum - currentSum) / (n - 1 - i)) + 1;
        currentSum += value;
        list.add(value);
    }

    list.add(sum - currentSum);
}

reference: Generate n random numbers whose sum is m and all numbers should be greater than zero

Hiep Le
  • 301
  • 8
1

Here is a possible solution:

First we can call our method that will get us our sum value.

Then we get a random number between 0 and the sum we calculated which will give our first number.

Subtract our first number from the sum get another random for the second and the final value will be the 2nd from the sum again.

//Get the random number between two values
public static int startRange(int x, int y) {
  Random rand = new Random();

  return rand.nextInt(Math.abs(x-y)) + Math.min(x, y);
}

public static void main(String[] args) {
   Random rand = new Random();

   int sum = startRange(30, 50);

   int firstNum = rand.nextInt(sum);

   sum -= firstNum;

   int secNum = rand.nextInt(sum);

   int thirdNum = sum - secNum;

   System.out.println(String.format("The nums are %d, %d and %d totaling %d", 
      firstNum, secNum, thirdNum, firstNum + secNum + thirdNum));
}
locus2k
  • 2,554
  • 1
  • 12
  • 17
  • The default `Random()` constructor is better than you to provide a seed, so let it do it for you and just write `Random rand = new Random();`. – Olivier Grégoire Jul 27 '20 at 17:00
0

I assume you want to generate a random combination of N integers such that—

  • each integer is 0 or greater,
  • the integers have a sum that lies in the interval [minsum, maxsum],
  • the integers appear in random order, and
  • the combination is chosen uniformly at random from among all combinations that meet the other requirements.

This can be described as—

  1. choosing a random sum (according to the number of combinations possible for that sum), then
  2. choosing a random combination for that sum.

Part 2 is trivial thanks to the Smith and Tromble algorithm, and I give Ruby code for this algorithm in a question for a related problem.

Part 1 is the trickier part. It involves—

  • counting the number of valid combinations for each sum in [minsum, maxsum] (more formally, the number of partitions of each sum into N parts, where each part can be empty and occur more than once), then
  • choosing a random sum with probability proportional to the number of valid combinations (partitions) for that sum.

This is only a sketch of the problem, since I don't know the exact formula that meets the first three requirements stated above in this answer. All I know is that the number of partitions of N into k non-empty parts is equal to the Sterling number of the second kind (but I don't know a similar formula for the case where the parts can be empty or the parts can occur more than once).

Peter O.
  • 28,965
  • 14
  • 72
  • 87
0

This is probably your best bet for a generic solution:

int[] randomNumbersWithSumBetween(int num, int min, int max, Random random) {
  if (min > max) throw new IllegalArgumentException("min > max");
  if (num < 1) throw new IllegalArgumentException("No random numbers to generate");
  int[] result = new int[num];
  result[0] = min + random.nextInt(max - min);
  for (; num-- > 1; ) {
    result[num] = random.nextInt(result[0]);
    result[0] -= result[num];
  }
  return result;
}
Olivier Grégoire
  • 28,397
  • 21
  • 84
  • 121