我该如何循环并重置变量

时间:2017-09-14 02:29:40

标签: java loops

当我尝试将for循环放入main()方法时,私有'iterations'变量似乎不会在每次循环运行时重置其值。每当for循环递增时,我需要私有变量重置为其原始值。

import java.util.Random;

public class RandomBuster {

    //used internally by Random to generate the next numbers                                                                                                                                                
    private static final long multiplier = 0x5DEECE66DL;
    private static final long addend = 0xBL;
    private static final long mask = (1L << 48) - 1;

    //the seed. If we find the seed, we can predict the 'random' numbers                                                                                                                                    
    private static Long seed;

    //the random number we will be busting                                                                                                                                                                  
    private static Random random = new Random();

    //how many iterations did we need?                                                                                                                                                                      
    private static int iterations = 1;

    public static void main(String[] args) {
       for (int x = 1; x <= 100; x++){
        findSeed(random.nextInt());
        System.out.println();

        //now that we know the seed, we can find the next integer                                                                                                                                           
        predictNext(32); // calculated seed is for v1. This gets us to v2                                                                                                                                   
        System.out.println("Predicted nextInt: " + predictNext(32));  //predicted v3                                                                                                                        
        System.out.println("   Random.nextInt: " + random.nextInt()); //actual v3 

    }}

    protected static synchronized void findSeed(long v1) {
        long v2 = random.nextInt();
        for (int i = 0; i < 65536; i++) {
            seed = v1 * 65536 + i;
            if ((((seed * multiplier + addend) & mask) >>> 16) == v2) {
                System.out.println();
                System.out.println("Seed found: " + seed + " in " + iterations + " iterations");
                break;
            }
            seed = null;
        }

        //if we haven't found it yet, loop through again                                                                                                                                                    
        iterations++;
        if (seed == null) findSeed(v2);
    }

    protected static synchronized int predictNext(int bits) {
        seed = (seed * multiplier + addend) & mask;
        return (int) (seed >>> (48 - bits));
    }

}

0 个答案:

没有答案