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

go评论76阅读模式

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

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

}
``````

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

}
``````

# 答案2

``````// 获取两个值之间的随机数
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

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

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

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

go 47

go 43

go 42

go 43