天天看點

java 僞Random

之前看了一篇文章,忘記了裡面有段代碼:

public static void main(String[] arg){

        System.out.println(randomString(-) + " " + randomString(-));
}

private static String randomString(int i) {

        Random ran = new Random(i);
        StringBuilder sb = new StringBuilder();

        while(true)
        {
            int k = ran.nextInt();
            if (k == )
                break;

            sb.append((char)('`' + k));
        }

        return sb.toString();
    }
           

輸出結果是: hello world

原理是java中的random是僞random,是以隻要 key固定,随機出的序列是固定的。

我不想深究其中原理,我想的是把一些字元串弄出來是不是很好玩。 于是有了下面的程式,為了友善,我把資料灌入方式寫在數組中。

public class RandomTest {

    private static int useSeconds = ;
    private static final int maxTime = ;
    private static boolean isRun = true;

    public static void main(String[] arg){

        System.out.println(randomString(-) + " " + randomString(-));

        int[] hello_world = {-, -};

        int[] i_love_you = {-,-,-};

        System.out.println(getString(hello_world));

        System.out.println(getString(i_love_you));

        // find random key
        String targetStr = "I Love You";
        findMyRandomkey(targetStr);
    }

    private static String getString(int[] keys){

        StringBuilder stringBuilder = new StringBuilder();

        for (int key : keys) {
            stringBuilder.append(randomString(key)).append(" ");
        }

        return stringBuilder.toString();
    }

    private static String randomString(int i) {

        Random ran = new Random(i);
        StringBuilder sb = new StringBuilder();

        while(true)
        {
            int k = ran.nextInt();
            if (k == )
                break;

            sb.append((char)('`' + k));
        }

        return sb.toString();
    }

    private static void findMyRandomkey(String targetStr){

        startTimer();

        String[] targetStrs = targetStr.toLowerCase().split(" ");

        StringBuilder stringBuilder = new StringBuilder("{");
        int result ;
        for(String str : targetStrs){
            byte[] e = str.getBytes();

            for(byte i= ;i<e.length; i++){
                e[i] -= '`';
            }


            if(Integer.MAX_VALUE != (result =findRandomkey(e))){

                stringBuilder.append(result).append(",");
            }
        }

        stringBuilder.delete(stringBuilder.length()-, stringBuilder.length());
        stringBuilder.append("}");

        System.out.println("result keys is : " + stringBuilder.toString());

        stopTimer();
    }

    private static int findRandomkey(byte[] ints){

        int maxValue = ;

        for (int value: ints) {

            if (maxValue < value)
                maxValue = value;
        }

        Random random;
        boolean isFind;

        for(int i = Integer.MIN_VALUE; i<Integer.MAX_VALUE; i++){

            if (isTimeOut()){
                return Integer.MAX_VALUE;
            }

            if(i== Integer.MAX_VALUE-)
                System.out.println(i);

            random = new Random(i);
            isFind = true;

            for (byte anInt : ints) {
                if (random.nextInt() != anInt) {
                    isFind = false;
                    break;
                }
            }

            if(isFind && random.nextInt() == ){
                return i;
            }
        }
        return Integer.MAX_VALUE;
    }

    private static void startTimer(){

        useSeconds = ;
        new Thread(new Runnable() {
            public void run() {

                while (isRun && !isTimeOut()){

                    System.out.println(" "  + useSeconds);

                    try {
                        Thread.sleep();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    useSeconds++;
                }
            }
        }).start();

    }

    private static void stopTimer(){
        isRun = false;
        System.out.println("共用時 " + useSeconds + " 秒");
    }

    private static boolean isTimeOut(){

        return useSeconds >= maxTime;
    }

}