当前位置: 技术问答>java相关
请给一个随机数产生的java程序?
来源: 互联网 发布时间:2015-05-10
本文导语: 请给一个随机数产生的java程序? 要求能通过触发产生一个随机数在1--100。一次触发产生一个输出,要多次触发时不能输出同样的数值。 万分感谢!! | GENERATING INTEGER RANDOM NUMBERS There...
请给一个随机数产生的java程序?
要求能通过触发产生一个随机数在1--100。一次触发产生一个输出,要多次触发时不能输出同样的数值。
万分感谢!!
要求能通过触发产生一个随机数在1--100。一次触发产生一个输出,要多次触发时不能输出同样的数值。
万分感谢!!
|
GENERATING INTEGER RANDOM NUMBERS
There are many ways to generate random numbers with the base libraries found in the Java 2 SDK, Standard Edition. If you haven't kept up to date with changes to the libraries, you might be using an inefficient mechanism or, possibly worse, getting results that are not uniformly distributed. Here's a look at a good way to generate integer random numbers, and then a look at what's not so good about some other ways.
The java.util.Random class has been available for generating random numbers since the initial release of the system libraries. Starting with the 1.2 release of the Java 2 SDK, Standard Edition, the Random class has a nextInt() method that accepts an integer argument:
public int nextInt(int n)
Given some value n, nextInt(n) returns a value greater than or equal to zero but less then that value: 0
There are many ways to generate random numbers with the base libraries found in the Java 2 SDK, Standard Edition. If you haven't kept up to date with changes to the libraries, you might be using an inefficient mechanism or, possibly worse, getting results that are not uniformly distributed. Here's a look at a good way to generate integer random numbers, and then a look at what's not so good about some other ways.
The java.util.Random class has been available for generating random numbers since the initial release of the system libraries. Starting with the 1.2 release of the Java 2 SDK, Standard Edition, the Random class has a nextInt() method that accepts an integer argument:
public int nextInt(int n)
Given some value n, nextInt(n) returns a value greater than or equal to zero but less then that value: 0