java产生随机数的方法

java生成随机数的三种方法
  • new Random()
  • Math.random()
  • currentTimeMillis()
1.new Random()

借助java.util.Random类产生一个随机数发生器,有两种构造函数

  1. Random() 以当前时间为默认种子
  2. Random(long seed) 以指定的种子值进行

产生之后,借助不同的语句产生不同类型的数

种子:产生随机数的第一次使用值 ,机制是通过一个函数,将这个种子的值转化为随机数空间中的某一个点上,并且产生的随机数均匀的散步再空间中,以后产生的随机数都与前一个随机数有关

//以指定的种子产生的5个数每次都一样,eg:我的均为 54 12 47 63 21
public static void main(String[] args){
    Random r = new Random(1);
    for(int i = 0 ; i < 5 ; i ++){
        int ran1 = r.nextInt(100);
        System.out.print(ran1 + " ");
    }
}

//采用Random r = new Random()产生的随机数就不同
public static void main(String[] args){
    Random r = new Random();
    for(int i = 0 ; i < 5 ; i ++){
        int ran1 = r.nextInt(100);
        System.out.print(ran1 + " ");
    }
}
2.Math.random()

返回的数值是[0.0 1.0)的double型数值 ,因double的精度很高,可以在一定程度上看作随机数,借助(int)来进行类型转化 就可以得到整数型随机数了

public static void main(String[] args){
    int max = 100 ,min = 1 ;
    int ran2 = (int)(Math.random()*(max-min)+min);
    System.out.print(ran2 + " ");
}
3.currentTimeMillis()

返回从1970年1月1日0时0分0秒到现在的一个long型毫秒数,取模之后即可得到所需范围内的随机数。但是不太常用。

public static void main(String[] args){
    int max = 100 , min = 1 ;
    long randomNum = System.currentTimeMillis();
    int ran3 = (int)(randomNum % (max - min ) + min );
    System.out.print(ran3 + " ");
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
% Known encoding formats are the following FDSN codes: % 0: ASCII % 1: 16-bit integer % 2: 24-bit integer (untested) % 3: 32-bit integer % 4: IEEE float32 % 5: IEEE float64 % 10: Steim-1 % 11: Steim-2 % 12: GEOSCOPE 24-bit (untested) % 13: GEOSCOPE 16/3-bit gain ranged % 14: GEOSCOPE 16/4-bit gain ranged (untested) % 19: Steim-3 (alpha and untested) % % See also MKMSEED to export data in miniSEED format. % % % Author: Franois Beauducel % Institut de Physique du Globe de Paris % Created: 2010-09-17 % Updated: 2012-04-21 % % Acknowledgments: % Ljupco Jordanovski, Jean-Marie Saurel, Mohamed Boubacar, Jonathan Berger, % Shahid Ullah. % % References: % IRIS (2010), SEED Reference Manual: SEED Format Version 2.4, May 2010, % IFDSN/IRIS/USGS, http://www.iris.edu % Trabant C. (2010), libmseed: the Mini-SEED library, IRIS DMC. % Steim J.M. (1994), 'Steim' Compression, Quanterra Inc. % History: % [2012-04-21] % - Correct bug with Steim + little-endian coding % (thanks to Shahid Ullah) % [2012-03-21] % - Adds IDs for warning messages % [2011-11-10] % - Correct bug with multiple channel name length (thanks to % Jonathan Berger) % [2011-10-27] % - Add LocationIdentifier to X.ChannelFullName % [2011-10-24] % - Validation of IEEE double encoding (with PQL) % - Import/plot data even with file integrity problem (like PQL) % [2011-07-21] % - Validation of ASCII encoding format (logs) % - Blockettes are now stored in substructures below a single % field X.BLOCKETTES % - Add import of blockettes 500 and 2000 % - Accept multi-channel files with various data coding % [2010-10-16] % - Alpha-version of Steim-3 decoding... % - Extend output parameters with channel detection % - Add gaps and overlaps on plots % - Add possibility to force the plot % [2010-10-02] % - Add the input formats for GEOSCOPE multiplexed old data files % - Additional output argument with gap and overlap analysis % - Create a plot when no output argument are specified % - Optimize script coding (30 times faster STEIM decoding!) % % [2010-09-28] % - Correction of a problem with STEIM-1 nibble 3 decoding (one % 32-bit difference) % - Add reading of files without blockette 1000 with additional % input arguments (like Seismic Handler output files). % - Uses warning() function instead of fprintf().

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值