views:

442

answers:

6

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

Xi = (aXi-1 + b) mod m

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

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];
+1  A: 

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.

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

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
Robert Gould
Thanks but i dont need the code, i'm new to java i need 2 know steps 2 follow.
Don't use 2, now I don't know if you need two steps to follow or need to know steps to follow
Robert Gould
@Robert Gould, Please revise your last sentence. I don't see how it's relevant, unless I am misunderstanding something.
strager
Thanks again Rogert, i can write a "hello world" program and i have actually written something that doesn't run, too (i won't use 2) many errors. i'll keep trying though.
@Strager, when people get homework sometimes this includes setting up their development environment. And Bongers said:"I'm new to Java", which means he might need help starting from that step, or not.
Robert Gould
can i show you wot i've done?
That would be best, then more people would be able to help in a meaningful way
Robert Gould
Robert, now i've posted it... how can you help me?
Downvoting an answer because of arguments is wrong. This was a valid answer and did not need downvoting...
Perchik
@edits:I think Bongers was naming his variable "X_[i]" not trying to use a list?
Perchik
that is possible with all things considered
Robert Gould
+4  A: 

Here are some hints:

int a, d, m, x;

Multiplication is * and mod is %.

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.

The important line of code will be

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

You don't need another x there because the x on 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.

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

Charlie Martin
+1  A: 

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's a 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.

You'll probably don't need to look into recursion like I first suggested.

Perchik
I really doubt it. You just need to keep the state in the class, recursion is probably next week's homework
Robert Gould
Well, the function deals with Xi and Xi-1, and a base case of X0. This would probably be how I introduced recursion, (if I was a professor).
Perchik
@Perchik, It's better solved with loops, IMO. Think of factorial with recursion. A better application of recursion is tree transversal.
strager
@Perchik, Also note that this is a random number generator. 'i' will continually be incremented. It's more efficiently handled using state variables (as opposed to recursion and recalculating i-1 items over and over and over again).
strager
@straeger Ah. I just wasn't understanding exactly what you were saying, I got it now. I feel like recursion makes sense by the way it was assigned, but a state variable does make it a lot more efficient. Iteration always beats recursion.
Perchik
This answer get downvoted and Cdonner's doesn't? I mean recursion is possible, but its an overkill
Robert Gould
Thanks Perchik, i'll try it out.
The eqn is actually X(i-1) ie using previous random number.
A: 

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

Can Berk Güder
A: 

Look at this.

cdonner
You shouldn't give code like this for homework you'll spoil the kids!
Robert Gould
Do we answer questions here or play games?
cdonner
Reversing the -1. @Gould, Read the guidelines on homework questions. Voting down a code answer to a homework question isn't too nice (in my eyes).
strager
(@cdonner, I would personally make your answer a CW one. It's not your solution, after all.)
strager
It helps if the asker understands the solution they ultimately use. Since this is obviously a homework question, it won't help the asker. It's better to explain the concepts and allow the person to come to the answer on their own terms.
Dan Herbert
@DanHerbert, This answer is a valid one. It's not wrong by any means, and should not be down voted.
strager
@DanHerbert et al., I understand your concerns, but SO is about answering programming questions, not about teaching kids how to learn. I think it would be difficult to draw such a line and be consequential.
cdonner
The contents are correct, but cdonner shouldn't have posted this in the first place, because it doesn't help anyone (in the sense of giving someone a fish, or teaching them to fish)
Robert Gould
Besides this answer is wrong, the question is about how to solve the issue, not a plzsendtehcodez question, and that's what cdonner just did..
Robert Gould
A: 

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

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

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 m to get that.

Having explained how it works, here's a simple Java program that implements it using values of a, b and m from your question:

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
paxdiablo