java随机生成随机数_用Java生成随机数

java随机生成随机数

Generating a series of random numbers is one of those common tasks that crop up from time to time. In Java, it can be achieved simply by using the java.util.Random class.

生成一系列随机数是不时出现的常见任务之一。 在Java中 ,只需使用java.util.Random类即可实现。

The first step, as with the use of any API class, is to put the import statement before the start of your program class:

与使用任何API 类一样 ,第一步是将import语句放在程序类开始之前:

Next, create a Random object:

接下来,创建一个Random对象:

The Random object provides you with a simple random number generator. The methods of the object give the ability to pick random numbers. For example, the nextInt() and nextLong() methods will return a number that is within the range of values (negative and positive) of the int and long data types respectively:

Random对象为您提供了一个简单的随机数生成器。 对象的方法可以选择随机数。 例如,nextInt()和nextLong()方法将分别返回int和long数据类型的值(负和正)范围内的数字:

The numbers returned will be randomly chosen int and long values:

返回的数字将是随机选择的int和long值:

从一定范围内挑选随机数 ( Picking Random Numbers From a Certain Range )

Normally the random numbers to be generated need to be from a certain range (e.g., between 1 to 40 inclusively). For this purpose, the nextInt() method can also accept an int parameter. It denotes the upper limit for the range of numbers. However, the upper limit number is not included as one of the numbers that can be picked. That might sound confusing but the nextInt() method works from zero upwards. For example:

通常,要生成的随机数必须在某个范围内(例如,包括1到40之间)。 为此,nextInt()方法还可以接受一个int参数。 表示数字范围的上限。 但是,上限编号不作为可以选择的编号之一。 这听起来可能令人困惑,但是nextInt()方法从零开始工作。 例如:

will only pick a random number from 0 to 39 inclusively. To pick from a range that starts with 1, simply add 1 to the result of the nextInt() method. For example, to pick a number between 1 to 40 inclusively add one to the result:

只会选择一个从0到39(含)之间的随机数。 要从以1开头的范围中进行选择,只需将nextInt()方法的结果加1。 例如,要选择一个介于1到40之间(包括1和40)的数字,请在结果中加上一个:

If the range starts from a higher number than one you will need to:

如果范围从大于1的数字开始,则需要:

  • minus the starting number from the upper limit number and then add one.

    从上限编号减去起始编号,然后再加一个。
  • add the starting number to the result of the nextInt() method.

    将起始编号添加到nextInt()方法的结果中。

For example, to pick a number from 5 to 35 inclusively, the upper limit number will be 35-5+1=31 and 5 needs to be added to the result:

例如,要选择一个介于5到35之间的数字,上限数字将为35-5 + 1 = 31,并且需要在结果中加上5:

随机类别的随机性如何? ( Just How Random Is the Random Class? )

I should point out that the Random class generates random numbers in a deterministic way. The algorithm that produces the randomness is based on a number called a seed. If the seed number is known then it's possible to figure out the numbers that are going to be produced from the algorithm. To prove this I'll use the numbers from the date that Neil Armstrong first stepped on the Moon as my seed number (20th July 1969) :​

我应该指出,Random类以确定性方式生成随机数。 产生随机性的算法基于一个称为种子的数字。 如果知道种子编号,则可以找出将要从算法生成的编号。 为了证明这一点,我将使用尼尔·阿姆斯特朗第一次踏上月球的日期作为我的种子编号(1969年7月20日):

No matter who runs this code the sequence of "random" numbers produced will be:

无论是谁运行此代码,产生的“随机”数字序列都将是:

By default the seed number that is used by:

默认情况下,以下对象使用的种子编号:

is the current time in milliseconds since January 1, 1970. Normally this will produce sufficiently random numbers for most purposes. However, note that two random number generators created within the same millisecond will generate the same random numbers.

是自1970年1月1日以来的当前时间(以毫秒为单位)。通常,对于大多数用途,这将产生足够的随机数。 但是,请注意,在同一毫秒内创建的两个随机数生成器将生成相同的随机数。

Also be careful when using the Random class for any application that must have a secure random number generator (e.g., a gambling program). It might be possible to guess the seed number based on the time the application is running. Generally, for applications where the random numbers are absolutely critical, it's best to find an alternative to the Random object. For most applications where there just needs to be a certain random element (e.g., dice for a board game) then it works fine.

将Random类用于必须具有安全随机数生成器的任何应用程序(例如,赌博程序)时,也要小心。 可能可以根据应用程序的运行时间猜测种子编号。 通常,对于随机数绝对重要的应用程序,最好找到随机对象的替代方法。 对于大多数只需要某个随机元素(例如,棋盘游戏的骰子)的应用程序来说,它可以正常工作。

翻译自: https://www.thoughtco.com/how-to-generate-random-numbers-2034206

java随机生成随机数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值