main.java

来自「java的经典例子」· Java 代码 · 共 39 行

JAVA
39
字号
import java.util.Random;

// Subclass that also has sequence generators for byte and short.
class NRandom extends Random {
    NRandom() {
    }

    NRandom(long seed) {
        super(seed);
    }

    public byte nextByte() {
        return (byte)next(8);   // Random low-order 8 bits
    }

    public short nextShort() {  // Random low-order 16 bits
        return (short)next(16);
    }
}

class Main {
    public static void main(String[] args) {
        NRandom rand = new NRandom(1997);

        // Let's see what the byte sequence looks like.
        for (int i = 0; i < 20; i++) {
            System.out.print(rand.nextByte() + " ");
        }
        System.out.println("\n");

        // Reset seed and try the short sequence.
        rand.setSeed(1997);
        for (int i = 0; i < 20; i++) {
            System.out.print(rand.nextShort() + " ");
        }
        System.out.println("\n");
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?