对于我用Java编写的程序,我应该从一个名为P1Random.java的类中获得一个随机值,这是一个随机数生成器,我的问题是:
我怎样才能让程序从另一个类中得到那个随机数呢?我只需要一个例子,比如:假设我希望随机数被显示到屏幕上。
数字生成器的代码在这里:
public class P1Random
{
private long next;
public P1Random()
{
next = 0;
}
private short nextShort()
{
return nextShort(Short.MAX_VALUE);
}
private short nextShort(short limit)
{
next = next * 1103515245 + 12345;
return (short) Math.abs(((next / 65536) % limit));
}
private int nextInt()
{
return nextInt(Integer.MAX_VALUE);
}
public int nextInt(int limit)
{
return ((((int) nextShort()) << 16) | ((int) nextShort())) % limit;
}
private double nextDouble()
{
return (double) nextInt() / (double) Integer.MAX_VALUE;
}
}
发布于 2018-06-12 12:45:42
误解了你的问题。请参阅上面提供的答案。
发布于 2018-06-12 13:56:31
使这些方法成为静态的:
/* Generate a random double between 0 and 1 */
public static double randomDouble() {
return new Random().nextDouble();
}
在任何地方使用:
public static void main(String... args) {
System.out.println(P1Random.randomDouble());
}
https://stackoverflow.com/questions/-100005323
复制相似问题