Java 常用类库 之 Random 随机数类

http://www.verejava.com/?id=169931089810102

import java.util.Random;

public class TestRandom {
    
    public static void main(String[] args) {
        //实例化随机数对象
        Random rn = new Random();
        for (int i = 0; i < 10; i++) {
            System.out.println(rn.nextInt(10));
        }
    }
}

//随机数产生验证码
public class Test {

    public static void main(String[] args) {
        // char[] chars={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        Random rn = new Random();
        for (int i = 0; i < 10; i++) {
            System.out.println(rn.nextInt());
        }

        //产生 [0 - 10) 之间的随机数
        for (int i = 0; i < 10; i++) {
            System.out.print(rn.nextInt(10) + ",");
        }

        char[] chars = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
        String validCode = "";
        for (int i = 0; i < 6; i++) {
            validCode += chars[rn.nextInt(26)]; //[0 - 26)
        }

        System.out.println("产生的随机数:" + validCode);
    }
}

http://www.verejava.com/?id=169931089810102

上一篇:Java 常用类库 之 Random 随机数类实例


下一篇:Java 常用类库 之 比较接口 Comparator