您的位置:首页 > 编程语言 > Java开发

《Java编程技巧1001条》359条:建立指定范围内的随机整数

2017-12-19 16:26 225 查看
《Java编程技巧1001条》359条:建立指定范围内的随机整数

359 Creating Random Integers for a SpecificRange 359 在一指定范围中创建随机数 The Random class methods for generating random numbers producevalues in the ranges indicated in Table 359: 生成随机数的 Random 类方法所产生的数值其范围如表 359 所示: 方法 名 称 输 出 范 围 nextFloat 0.0 to1.0 nextDouble
0.0 to 1.0 nextInt -2147483648 to 2147483647 nextLong-9223372036854775808 to 9223372036854775807 Table 359 Range of random numbers. 表 359 随机数的范围 As youlearned in Tip 360, by performing simple math operations, you can manipulatethe random numbers to produce
a desired range. The following example shows youhow you might limit the range of floating-point random numbers from 0 to 100: 正如你在TIP 360中所了解的,借助于简单的数学运算,你就可以控制随机数产生在某一个 希望的范围之内. 以下例子向你演示怎样把浮点随机数的范围限制在 0 到 100 之内: Random r = new Random(); // create a Random
object float f =r.nextFloat(); // get a random value (0.0 to 1.0) float v = (f * 100); //multiply by a factor to get 0 to 100 【本章完,下一章待续】 - 14 -1001 Java Tips PAGE 29 of numpages 35 filename jt0375_a TIME \@ "MMMM d,yyyy" November 24, 1996 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: