Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 96a4229

Browse filesBrowse files
committed
Merge pull request giantray#5 from peiquan/master
generating-random-integers-in-a-range-with-Java.md
2 parents 52b4a9c + ce97a7c commit 96a4229
Copy full SHA for 96a4229

File tree

Expand file treeCollapse file tree

1 file changed

+94
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+94
-0
lines changed
+94Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# java 产生指定范围的随机数
2+
问题,如何使用 java 产生 0~10,5~10 之间的随机数?
3+
4+
## [Math.random()](http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#random--)
5+
6+
Math.random() 可以产生一个 **大于等于** 0 且 **小于** 1 的双精度伪随机数,假设需要产生 ”0《= 随机数 <=10” 的随机数,可以这样做:
7+
8+
```
9+
int num =(int)(Math.random() * 11);
10+
11+
```
12+
13+
那如何产生 “5《= 随机数 <= 10” 的随机数呢?
14+
15+
```
16+
int num = 5 + (int)(Math.random() * 6);
17+
18+
```
19+
20+
生成 “min <= 随机数 <= max ” 的随机数
21+
22+
```
23+
int num = min + (int)(Math.random() * (max-min+1));
24+
25+
```
26+
27+
28+
## [java.util.Random](http://docs.oracle.com/javase/8/docs/api/java/util/Random.html)
29+
30+
Random 是 java 提供的一个伪随机数生成器。
31+
32+
生成 “min <= 随机数 <= max ” 的随机数:
33+
34+
```
35+
import java.util.Random;
36+
37+
/**
38+
* Returns a pseudo-random number between min and max, inclusive.
39+
* The difference between min and max can be at most
40+
* <code>Integer.MAX_VALUE - 1</code>.
41+
*
42+
* @param min Minimum value
43+
* @param max Maximum value. Must be greater than min.
44+
* @return Integer between min and max, inclusive.
45+
* @see java.util.Random#nextInt(int)
46+
*/
47+
public static int randInt(int min, int max) {
48+
49+
// NOTE: Usually this should be a field rather than a method
50+
// variable so that it is not re-seeded every call.
51+
Random rand = new Random();
52+
53+
// nextInt is normally exclusive of the top value,
54+
// so add 1 to make it inclusive
55+
int randomNum = rand.nextInt((max - min) + 1) + min;
56+
57+
return randomNum;
58+
}
59+
60+
```
61+
62+
## 标准库
63+
64+
在实际使用中,没有必要区重新写一次这些随机数的生成规则,可以借助一些标准库完成。如 [commons-lang](https://commons.apache.org/proper/commons-lang/index.html).
65+
66+
org.apache.commons.lang3.RandomUtils 提供了如下产生指定范围的随机数方法:
67+
68+
```
69+
// 产生 start <= 随机数 < end 的随机整数
70+
public static int nextInt(final int startInclusive, final int endExclusive);
71+
// 产生 start <= 随机数 < end 的随机长整数
72+
public static long nextLong(final long startInclusive, final long endExclusive);
73+
// 产生 start <= 随机数 < end 的随机双精度数
74+
public static double nextDouble(final double startInclusive, final double endInclusive);
75+
// 产生 start <= 随机数 < end 的随机浮点数
76+
public static float nextFloat(final float startInclusive, final float endInclusive);
77+
78+
```
79+
80+
org.apache.commons.lang3.RandomStringUtils 提供了生成随机字符串的方法,简单介绍一下:
81+
82+
```
83+
// 生成指定个数的随机数字串
84+
public static String randomNumeric(final int count);
85+
// 生成指定个数的随机字母串
86+
public static String randomAlphabetic(final int count);
87+
// 生成指定个数的随机字母数字串
88+
public static String randomAlphanumeric(final int count);
89+
90+
```
91+
92+
stackoverflow原址:http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java
93+
文章若有写得不正确或不通顺的地方,恳请你指出,谢谢。
94+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.