zoukankan      html  css  js  c++  java
  • java产生随机数方法

     System.nanoTime提供相对精确的计时,但是不能用来计算当前日期,因为可能为负数,但是计算一段程序跑了多长时间还是可以的,哪怕是负数,差值是一定的

    import java.util.Random;
    
    public class RandomTest {
    	public static void main(String[] args) {
    		/*不用currentTimeMillis的原因是:当多线程调用时,由于CPU速率很快,
    		 * 因此currentTimeMillis很可能相等,使得随机数结果也会相等。
    //      long seed1 = System.currentTimeMillis();
    //nanoTime()返回最准确的可用系统计时器的当前值,以毫微秒为单位(即纳秒)。此方法只能用于测量已过的时间,与系统或钟表时间的其他任何时间概念无关。*/
    		Random r = new Random(System.nanoTime());
    		for (int i = 1; i <= 100; ++i)
    		{
    			System.out.print(r.nextInt(100) + " ");//不要扩大倍数再取整,推荐用自带方法,取100以内随机整数
    			if (i % 10 == 0) {
    				System.out.println();
    			}
    		}
    		System.out.println();
    		for (int i = 1; i <= 100; ++i)
    		{
    			System.out.print(r.nextBoolean() + " ");
    			if (i % 10 == 0) {
    				System.out.println();
    			}
    		}
    	}
    }
    ========================================Talk is cheap, show me the code=======================================
    CSDN博客地址:https://blog.csdn.net/qq_34115899
  • 相关阅读:
    BZOJ2337 [HNOI2011]XOR和路径
    「学习笔记」3.31代码学习
    uva live 12846 A Daisy Puzzle Game
    Cannot use ImageField because Pillow is not installed
    Android点击Button水波纹效果
    hdu 1241 Oil Deposits
    c++ 字符输入读取
    clutter recoder
    C/C++获取数组长度
    vector array and normal stanard array
  • 原文地址:https://www.cnblogs.com/lcy0515/p/9179829.html
Copyright © 2011-2022 走看看