Java – random number generator
•
Java
I need to write a program in Java to generate random numbers within the range of [0,1] using the following formula:
Suppose a, B and amp; Any fixed int value of M and x0 = 0.5 (i.e. I = 0)
What should I do?
I've tried 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];
Solution
Here are some tips:
int a,d,m,x;
Multiplication is *, mod is%
to update
OK, I'll give you another hint You only need an X, you don't need all these arrays; Because you only use integers, you don't need any floats or double arrows
The important lines of code are
x = (a * x + b) % m ;
You don't need another x, because = x on the right is old X or XI-1; The one on the left will be your "new" X or Xi
Now, from there, you need to write a Java wrapper that lets you create a method, which means writing a class
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码