开发者社区> 问答> 正文

java怎么随机生成整数

java怎么随机生成整数

展开
收起
云计算小粉 2018-05-10 20:09:05 4285 0
3 条回答
写回答
取消 提交回答
  • new Random().nextInt();

    2019-09-09 10:53:47
    赞同 展开评论 打赏
  • 精于基础,广于工具,熟于业务。
    public static void main(String[] args) {
    	for (int i = 0; i < 10; i++) {
    		System.out.print((int)(Math.random()*100) + " ");
    	}		System.out.println("/n" + "-----------------------------");
    	// 每次的随机数不同
    	Random rand = new Random();
    	for (int i = 0; i < 10; i++) {
    		System.out.print(rand.nextInt(100) + " ");
    	}
    	System.out.println("/n" + "-----------------------------");
    	// 带种子,每次的随机数相同
    	Random rand2 = new Random(10);
    	for (int i = 0; i < 10; i++) {
    		System.out.print(rand2.nextInt(100) + " ");
    	}
    	// 生成完全不重复的整数	
              SecureRandom random = new SecureRandom();
    	for (int i = 0; i < 10; i++) {
    		System.out.print(random.nextInt(100) + " ");
    	}
    }
    
    2019-09-05 19:47:49
    赞同 展开评论 打赏
  • new Random().nextInt()
    2019-07-17 22:18:11
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载