mirror of
https://github.com/corda/corda.git
synced 2025-01-07 13:38:47 +00:00
Implemented java.lang.Math.random() properly (seeding the random number
on first use, and then using the system random number generator)
This commit is contained in:
parent
d24b633665
commit
51c198f0f0
@ -136,6 +136,26 @@ Java_java_lang_Math_pow(JNIEnv*, jclass, jdouble val, jdouble exp)
|
|||||||
return pow(val, exp);
|
return pow(val, exp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern "C" JNIEXPORT void JNICALL
|
||||||
|
Java_java_lang_Math_natRandomInitialize(JNIEnv*, jclass, jlong val)
|
||||||
|
{
|
||||||
|
#ifdef WIN32
|
||||||
|
srand(val);
|
||||||
|
#else
|
||||||
|
srand48(val);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" JNIEXPORT jdouble JNICALL
|
||||||
|
Java_java_lang_Math_natRandom(JNIEnv*, jclass)
|
||||||
|
{
|
||||||
|
#ifdef WIN32
|
||||||
|
return rand();
|
||||||
|
#else
|
||||||
|
return drand48();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
extern "C" JNIEXPORT jdouble JNICALL
|
extern "C" JNIEXPORT jdouble JNICALL
|
||||||
Java_java_lang_Math_floor(JNIEnv*, jclass, jdouble val)
|
Java_java_lang_Math_floor(JNIEnv*, jclass, jdouble val)
|
||||||
{
|
{
|
||||||
|
@ -3,6 +3,7 @@ package java.lang;
|
|||||||
public final class Math {
|
public final class Math {
|
||||||
public static final double E = 2.718281828459045;
|
public static final double E = 2.718281828459045;
|
||||||
public static final double PI = 3.141592653589793;
|
public static final double PI = 3.141592653589793;
|
||||||
|
private static boolean randomInitialized = false;
|
||||||
|
|
||||||
private Math() { }
|
private Math() { }
|
||||||
|
|
||||||
@ -62,7 +63,17 @@ public final class Math {
|
|||||||
return (int) Math.floor(v + 0.5);
|
return (int) Math.floor(v + 0.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static native double random();
|
public static double random() {
|
||||||
|
if (randomInitialized) {
|
||||||
|
natRandomInitialize(System.currentTimeMillis());
|
||||||
|
randomInitialized = true;
|
||||||
|
}
|
||||||
|
return natRandom();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static native void natRandomInitialize(long val);
|
||||||
|
|
||||||
|
public static native double natRandom();
|
||||||
|
|
||||||
public static native double floor(double v);
|
public static native double floor(double v);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user