《Java编程技巧1001条》358条:控制随机整数的范围
2017-12-19 16:21
246 查看
《Java编程技巧1001条》358条:控制随机整数的范围
358Controlling the Range of Random Integers 358 控制随机数的范围 As you have learned, Java praograms cangenerate random integers by using an instance of the Random class. The Randomclass contains a method called nextInt, which produces a new random int foreach call.
Depending on your programs purpose, you may need to produce randomnumbers - 13 - in a specific range. To control the rnge of integer values arandom-number generator creates, you can use the modulus operator (%). Forexample, if you need numbers in the range
-100 to 100, you can get the remainderof the random number divided by 100, as shown: 正如你已了解的, Java 程序是利用 random 类的一个实例来生成随机数. random 类包含一 个称作 NexrInt 的方法,它为每一次调用产生一个新的随机整型数. 按照你的程序的用途,你可 能需要产生某一范围内的随机数. 为了控制一随机数建立的整数的范围,你可以使用取模运算 符(%). 例如, 若需要 -100 到 100 范围中的数,
可用 100 来除随机数, 求出其余数, 如下: Random r = new Random(); // use a defaultinitial seed System.out.println(Random int is + r.nextInt() % 100); Likewise,if you need to restrict the range of numbers to positive values, say 0 to 100,your programs can use the Math class
abs function, as shown: 同样,如果你要把数值限制在正数范围之内,例如 0 到 100,则你可以在程序中使用 Math 类的 sub 函数,如下所示: Random r = new Random(); // use a defaultinitial seed System.out.println(Random int is + Math.abs(r.nextInt() % 100));
358Controlling the Range of Random Integers 358 控制随机数的范围 As you have learned, Java praograms cangenerate random integers by using an instance of the Random class. The Randomclass contains a method called nextInt, which produces a new random int foreach call.
Depending on your programs purpose, you may need to produce randomnumbers - 13 - in a specific range. To control the rnge of integer values arandom-number generator creates, you can use the modulus operator (%). Forexample, if you need numbers in the range
-100 to 100, you can get the remainderof the random number divided by 100, as shown: 正如你已了解的, Java 程序是利用 random 类的一个实例来生成随机数. random 类包含一 个称作 NexrInt 的方法,它为每一次调用产生一个新的随机整型数. 按照你的程序的用途,你可 能需要产生某一范围内的随机数. 为了控制一随机数建立的整数的范围,你可以使用取模运算 符(%). 例如, 若需要 -100 到 100 范围中的数,
可用 100 来除随机数, 求出其余数, 如下: Random r = new Random(); // use a defaultinitial seed System.out.println(Random int is + r.nextInt() % 100); Likewise,if you need to restrict the range of numbers to positive values, say 0 to 100,your programs can use the Math class
abs function, as shown: 同样,如果你要把数值限制在正数范围之内,例如 0 到 100,则你可以在程序中使用 Math 类的 sub 函数,如下所示: Random r = new Random(); // use a defaultinitial seed System.out.println(Random int is + Math.abs(r.nextInt() % 100));
相关文章推荐
- 《Java编程技巧1001条》359条:建立指定范围内的随机整数
- 编程珠玑: 13章 搜索 13.2使用线性结构,生成[0 ,maxval]范围内m各随机整数的有序序列 -------解题总结
- //linux shell 生成 范围 随机整数
- 在1~10的整数范围随机5个不重复的整数
- 编程珠玑: 13章 搜索 13.4使用整数结构,生成[0 ,maxval]范围内m各随机整数的有序序列 -------解题总结
- 编写函数实现随机产生指定范围的整数的功能
- 生成固定范围不重复随机整数(JS&PHP)
- 生成范围在[a,b]之中的随机整数
- mysql rand()产生随机整数范围及方法
- 编写一个javscript函数 fn,该函数有一个参数 n(数字类型),其返回值是一个数组,该数组内是 n 个随机且不重复的整数,且整数取值范围是 [2, 32]。
- java 随机生成18到25范围内的整数
- Math.random() 生成指定范围随机整数的思考
- 生成范围在[a,b]之中的随机整数
- JS 浅谈生成指定范围内随机整数的五种方法
- 指定范围的随机整数的获取方法
- 编程珠玑: 13章 搜索 13.1生成[0 ,maxval]范围内m各随机整数的有序序列 -------解题总结
- mysql给某字段随机赋特定范围的整数值
- JAVA产生指定范围的随机整数
- 如何获取一个范围内的随机整数
- 用Int和Rnd函数生成指定范围的随机整数