java 随机数发生器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/648976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:12:52  来源:igfitidea点击:

Random Number Generator

javarandom

提问by Robert Gould

I need to write a program in Java to generate random numbers within the range [0,1] using the formula:

我需要用 Java 编写一个程序来使用以下公式生成 [0,1] 范围内的随机数:

Xi= (aXi-1+ b) mod m

X i= (aX i-1+ b) mod m

assuming any fixed int values of a, b & m and X0= 0.5 (ie i=0)

假设 a, b & m 和 X 0= 0.5 (即 i=0) 的任何固定 int 值

How do I go about doing this?

我该怎么做?

i tried doing this but it's obviously wrong:

我尝试这样做,但显然是错误的:

int a = 25173, b = 13849, m = 32768;
double X_[i];
for (int i = 1; i<100; i++)
   X_[i] = (a*(X_[i]-1) + b) % m;
double X_[0] = 0.5;
double double = new double();
System.out.println [new double];

回答by Charlie Martin

Here are some hints:

这里有一些提示:

int a, d, m, x;

Multiplication is *and modis %.

乘法是*mod%

update

更新

Okay, I'll give you a little more of a hint. You only need one X, you don't need all these arrays; since you're only using integers you don't need any floats or doublts.

好吧,我再给你一点提示。你只需要一个 X,你不需要所有这些数组;由于您只使用整数,因此您不需要任何浮点数或浮点数。

The important line of code will be

重要的代码行将是

x = (a * x + b) % m ;

You don't need another xthere because the xon the right hand side of the =is the OLD x, or xi-1; the one on the left side will be your "new" x, or xi.

你不需要另一个x,因为x右边=是 OLD x,或x i-1;左侧的将是您的“新”xx i

Now, from there, you need to write the Java wrapper that will let you make that a method, which means writing a class.

现在,从那里开始,您需要编写 Java 包装器,让您可以将其作为方法,这意味着编写一个

回答by Robert Gould

Sounds like homework... so I won't give you a solution in code.

听起来像家庭作业……所以我不会给你代码中的解决方案。

Anyways you need a linear congruential generator.

无论如何,您需要一个线性同余生成器

HINT:You need to write that mathematical formula as a function.

提示:您需要将该数学公式写为函数。

Steps:

脚步:

  1. Make a class.
  2. Add the required state as member to the class.
  3. Make a function within the class. Have it take input as necessary.
  4. Write the formula for the congruential generator in Java (look up math operations in Java).
  5. Return the result.
  1. 做一堂课。
  2. 将所需的状态作为成员添加到类中。
  3. 在类中创建一个函数。让它在必要时接受输入。
  4. 用 Java 编写同余生成器的公式(查找 Java 中的数学运算)。
  5. 返回结果。

My Java is rusty, so I can't say I'm sure about this but these are probably errors:

我的 Java 生锈了,所以我不能说我对此有把握,但这些可能是错误:

int a = 25173, b = 13849, m = 32768;
double X_[i];//You need to define a constant array or use perhaps a list, you can't use i without defining it
for (int i = 1; i<100; i++)
   X_[i] = (a*(X_[i]-1) + b) % m;
double X_[0] = 0.5;
double double = new double(); //You can't name a variable double, also types like double, don't need to be newed (I think)
System.out.println [new double]; //println uses () not [], in Java I think all functions need to use (), its not implied

回答by Perchik

EDIT: Bongers:

编辑:邦格斯:

  1. [ ] are special symbols, if you intended for your variable to be named "X_[ i ]" that won't work. If you intended to make an array, you're making it too complicated.

  2. You need to figure out if theY original equation was Xi - 1 or X(i-1) as that makes a huge difference in your programming. Xi - 1 is just one less than Xi. X(i-1) is the previous random number.

  3. try doing some beginner java tutorials online. Here'sa good place to start. Really try to understand the tutorials before continuing on to your problem.

  4. Think about your problem this way.[Assuming the equation is X(i-1)] To generate the 3rd random number, X3, you will need to generate X2, which needs X1, which needs X0. But you have X0. So for any Xi, start with X0, generate X1, then generate X2, etc.. up until Xi.

  1. [ ] 是特殊符号,如果您打算将变量命名为“X_[ i ]”,那将不起作用。如果你打算制作一个数组,你就让它太复杂了。

  2. 您需要弄清楚 Y 的原始方程是 Xi - 1 还是 X(i-1) ,因为这会对您的编程产生巨大影响。Xi - 1 仅比 Xi 小 1。X(i-1) 是前一个随机数。

  3. 尝试在线做一些初级 Java 教程。这是一个很好的起点。在继续解决您的问题之前,请真正尝试理解教程。

  4. 这样想想你的问题。[假设方程是 X(i-1)] 要生成第三个随机数 X3,你需要生成 X2,它需要 X1,它需要 X0。但是你有X0。因此,对于任何 Xi,从 X0 开始,生成 X1,然后生成 X2,依此类推,直到 Xi。

You'll probably don'tneed to look into recursionlike I first suggested.

您可能不需要像我最初建议的那样研究递归

回答by paxdiablo

A linear congruential generator is basically an expression which modifies a given value to produce the next value in the series. It takes the form:

线性同余生成器基本上是一个表达式,它修改给定值以生成系列中的下一个值。它采用以下形式:

xi+1= (a.xi+ b) mod m

x i+1= (a .x i+ b) mod m

as you've already specified (slightly differently: I was taught to always put xi+1on the left and I still fear my math teachers 25 years later :-), where values for a, band mare carefully chosen to give a decent range of values. Note that with the modoperator, you will always end up with a value between 0and m-1inclusive.

如您已指定(略有不同:我被教导要始终把X i + 1的左边,我仍然担心我的数学老师25年后:-),那里的值ab并且m都经过精心挑选给一个体面的范围值。请注意,使用mod运算符,您将始终得到一个介于0和之间的值m-1

Note also that the values tend to be integral rather than floating point so if, as you request, you need a value in the range 0-0.999..., you'll need to divide the integral value by mto get that.

另请注意,这些值往往是整数而不是浮点数,因此如果按照您的要求,您需要一个范围在 0-0.999 之间的值...,您需要将整数值除以m得到它。

Having explained how it works, here's a simple Java program that implements it using values of a, band mfrom your question:

在解释它是如何工作的,这里有一个简单的Java程序,它使用的价值它实现abm从你的问题:

public class myRnd {
    // Linear congruential values for x(i+1) = (a * x(i) + b) % m.
    final static int a = 25173;
    final static int b = 13849;
    final static int m = 32768;

    // Current value for returning.
    int x;

    public myRnd() {
        // Constructor simply sets value to half of m, equivalent to 0.5.
        x = m / 2;
    }

    double next() {
        // Calculate next value in sequence.
        x = (a * x + b) % m;

        // Return its 0-to-1 value.
        return (double)x / m;
    }

    public static void main(String[] args) {
        // Create a new myRnd instance.
        myRnd r = new myRnd();

        // Output 20 random numbers from it.
        for (int i = 0; i < 20; i++) {
            System.out.println (r.next());
        }
    }   
}

And here's the output, which looks random to me anyway :-).

这是输出,无论如何对我来说都是随机的:-)。

0.922637939453125
0.98748779296875
0.452850341796875
0.0242919921875
0.924957275390625
0.37213134765625
0.085052490234375
0.448974609375
0.460479736328125
0.07904052734375
0.109832763671875
0.2427978515625
0.372955322265625
0.82696533203125
0.620941162109375
0.37451171875
0.006134033203125
0.83465576171875
0.212127685546875
0.3128662109375

回答by BigGinDaHouse

public class generate_random_numbers {

    public static void main(String[] args) {
        int a = 25173, b = 13849, m = 32768;
        Double[] X_ = new Double[100];
        X_[0] = 0.5;
        for (int i = 1; i < 100; i++) {
            X_[i] = (a * X_[i - 1] + b) % m;
            X_[i] = X_[i] / m;
            System.out.println("X_[" + i + "] = " + X_[i]);
        }
    }
}

回答by Can Berk Güder

I would start by creating a class that holds a, b, m, the latest x (initialized to 0.5), and a method like getNextNumber().

我将首先创建一个包含 a、b、m、最新的 x(初始化为 0.5)的类和一个像 getNextNumber() 这样的方法。