美文网首页
java中随机数的几种取法

java中随机数的几种取法

作者: 不知不怪 | 来源:发表于2018-07-12 18:02 被阅读0次
package com.gzz.createcode;

import java.util.Random;

public class Number {
    private static Random random = new Random();

    public static void main(String[] args) {
        System.out.println("随机6位数范围(0~999999)");
        for (int j = 0; j < 30; j++) {
            System.out.printf("%06d\t", random.nextInt(1000000));
        }

        System.out.println("\r\n随机6位数范围(0~999999)");
        for (int j = 0; j < 30; j++) {
            System.out.printf("%06d\t", (int) (Math.random() * 1000000));
        }

        System.out.println("\r\n首位非零随机6位数");
        for (int j = 0; j < 30; j++) {
            System.out.print(100000 +(int)( Math.random() * 900000) + "\t");
        }
        System.out.println("\r\n取100到200之间的随机整数");
        for (int j = 0; j < 30; j++) {
            System.out.print(100 +(int)(Math.random() * 201) + "\t");
        }
            System.out.println(new DecimalFormat("0000").format(Math.random() * 10000));
    }
}

相关文章

网友评论

      本文标题:java中随机数的几种取法

      本文链接:https://www.haomeiwen.com/subject/afirpftx.html