0

So I'm trying to convert this bit of code to use in my Java program. However, it has a pointer and I'm not sure what would be the best way of translating that into Java.

/**
* 32-bit Multiplicative Congruential Pseudo Random Number Generator
* Microsoft Visual C++ Version
*/
#include <math.h>
/**
* The seed used by randomNumberGenerator.
*/
long gv_lRandomNumberSeed;
/**
* Returns a random number between 0.0 and 1.0.
* plSeed - Pointer to the seed value to use.
*/
double randomNumberGenerator( long* plSeed ) {
double dZ;
double dQuot;
long lQuot;
dZ = ( *plSeed ) * 16807;
dQuot = dZ / 2147483647;
lQuot = ( long ) floor( dQuot );
dZ -= lQuot * 2147483647;
( *plSeed ) = ( long ) floor( dZ );
return ( dZ / 2147483647 );
}

I need to be able to use the above number returned in this function:

/**
* Returns a random variate from an exponential probability
* distribution with the given mean value of dMean.
*/
double exponentialRVG( double dMean ) {
return ( -dMean * log( randomNumberGenerator( &gv_lRandomNumberSeed 
) ) );
}

And be able to enter a value for dMean and get a value in return..

Any help?

(I know a lot of people get angry about poorly posted questions on this site, please try and refrain from the negatively and help me to ask the question better if that is the case)

5 Answers 5

2

The pointer is used to make plSeed an inout variable.

As you will already be putting randomNumberGenerator into some class, you should promote the function parameter plSeed to a field of that class.

Sign up to request clarification or add additional context in comments.

2 Comments

If plSeed is a field, what value am I supposed to assign to it? The first function is supposed to produce a value between 0 and 1 just from me calling the function.
One option is to initialize the seed to something based on the current time -- e.g., System.currentTimeMillis().
1

The pointer is being used to update the value of an argument. I'd suggest reorganizing the code to avoid using such an argument (and directly access a class variable gv_lRandomNumberSeed). The rest of the code translates fairly directly into Java (if you're familiar with Java's Random class).

Oh, and please get rid of the Hungarian notation when you port.

2 Comments

Yea the rest of the code seems pretty straight forward. But I'm not sure what the intended input value of "gv_lRandomNumberSeed" is supposed to be. The two pieces of code together are supposed to be able to function solely from the insertion of a Mean value that I code in. If I make "gv_1RandomNumberSeed" a field, I'm not sure what value I'm supposed to assign to it.
@GasperGulotta - My guess is that there's some initialization function for the PRNG that sets the initial value. It probably doesn't matter too much what it is (provided it's a positive value). Note that the result will be between 0.0 and 1.0 because 2147483647 is the largest long value possible and dZ is strictly less than that by construction.
1

Choices include:

  • Place the seed as a field in the same class as the method randomNumberGenerator().
  • Pass the seed in a mutable object parameter to randomNumberGenerator().
  • Return the seed in an object that contains both the new value and the new seed.

Java's Random class uses the first approach. It's the easiest for callers of the method.

Comments

0

Use JNI(Java Native Interface) technology. With JNI, you can call the C function from java.

2 Comments

For this small amount of code, JNI is likely to be more expensive in both in development costs and at runtime.
Please refer the Wiki's link to understant the basics of JNI. link
0

That pointer is basically just the C equivalent of a reference parameter. You can achieve the same thing in Java in a number of ways. Have a look at the answers in the question below:

Can I pass parameters by reference in Java?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.