生成1到60之间的随机数字。

huangapple go评论134阅读模式
英文:

How to generate random numbers between 1 and 60

问题

我知道如何生成随机数,但不知道如何在固定范围内生成。

英文:

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

答案1

得分: 2

为什么不只是简单地随机生成一个总和,然后生成 n 个总和为该值的随机数。

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);
}

参考链接:https://stackoverflow.com/questions/22380890/generate-n-random-numbers-whose-sum-is-m-and-all-numbers-should-be-greater-than

英文:

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&lt;Integer&gt; list = new ArrayList&lt;&gt;();
    int currentSum = 0;
    for (int i = 0; i &lt; n - 1; i++) {
        int value = random.nextInt((int) (sum - currentSum) / (n - 1 - i)) + 1;
        currentSum += value;
        list.add(value);
    }

    list.add(sum - currentSum);
}

reference: https://stackoverflow.com/questions/22380890/generate-n-random-numbers-whose-sum-is-m-and-all-numbers-should-be-greater-than

答案2

得分: 1

以下是可能的解决方案:

首先,我们可以调用我们的方法,该方法将获取我们的总和值。

然后,我们获取一个介于0和我们计算的总和之间的随机数,这将给出我们的第一个数字。

从总和中减去第一个数字,然后再获取另一个随机数作为第二个数字,最终值将再次从总和中获得。

// 获取两个值之间的随机数
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("这些数字是 %d, %d 和 %d,总计 %d", 
      firstNum, secNum, thirdNum, firstNum + secNum + thirdNum));
}
英文:

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(&quot;The nums are %d, %d and %d totaling %d&quot;, 
      firstNum, secNum, thirdNum, firstNum + secNum + thirdNum));
}

答案3

得分: 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).

英文:

I assume you want to generate a random combination of N integers such that&mdash;

  • 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&mdash;

  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&mdash;

  • 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).

答案4

得分: 0

这可能是您获得通用解决方案的最佳选择:

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;
}
英文:

This is probably your best bet for a generic solution:

int[] randomNumbersWithSumBetween(int num, int min, int max, Random random) {
  if (min &gt; max) throw new IllegalArgumentException(&quot;min &gt; max&quot;);
  if (num &lt; 1) throw new IllegalArgumentException(&quot;No random numbers to generate&quot;);
  int[] result = new int[num];
  result[0] = min + random.nextInt(max - min);
  for (; num-- &gt; 1; ) {
    result[num] = random.nextInt(result[0]);
    result[0] -= result[num];
  }
  return result;
}

huangapple
  • 本文由 发表于 2020年7月27日 23:37:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63118668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定