samedi 23 octobre 2021

Modify Java Random with reflection [duplicate]

Java reflection is powerful, I know it can SET declared fields or invoke method with new parameters.
Now can we "hack" the Random class like below to be "true" everytime?
System.out.println( java.lang.Math.random() == java.lang.Math.random() );

Math.random()

    public static double random() {
        return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();}

And the nextDouble()

     public double nextDouble() {
        return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT;}

And I'm stuck here, these methods don't even use any parameter to tamper with and I don't know if Java reflection can mess around inside method. Is there anyway else to get the same Math.random() every time?





Aucun commentaire:

Enregistrer un commentaire