Random

一、Random

1、创建Random类型的对象:

 

Random random = new Random();//默认构造方法

Random random = new Random(10010010);//指定种子数字

 

2、生成随机数字:

 

生成随机整数:

int k = random.nextInt();

 

生成随机长整数:

long l = random.nextLong();

 

3、生成指定范围的数字:

 

例如生成0-10之间的随机数字:

 int k = random.nextInt();

int j = Math.abs(k % 10);

 

//直接方式:rand.nextInt(range);

int i =random.nextInt(10);

 

获得10-20之间的随机数字:

int k = random.nextInt();

int j = Math.abs(k % 10) + 10;

 

int i = (int)(Math.random()*1000)

 

random对象的nextInt(),nextInt(int n)说明:

 

int nextInt()
  返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。  有正有负。

int nextInt(int n)
  返回一个伪随机数,它是从此随机数生成器的序列中取出的、在 0(包括)和指定值(不包括)之间均匀分布的 int值。

 

4、next(int bits)

 

        next(n)产生2的n次方之内的随机数,next(32)产生2的32次方之内的随机数,就相当于nextInt()了。

 

5、setSeed(long seed)

 

       setSeed(long seed) 用于设定随机数的种子,即这里的seed。随机数种子的用处是:一般说来,这里的Random类产生随机数是伪随机数,是系统采用特定的算法生成出来的,方法是new两个Random类random1和random2。各自调用nextInt方法10次,我们可以看出,虽然各自产生的是随机数,但是两个Random类产生的随机数都是一样的。这就使得随机数存在着漏洞。

 

二、java disabuse

 

Java代码 复制代码
  1. /***********双括弧初始化:内层的花括号就是匿名类的初始化子句************/  
  2. @SuppressWarnings("unchecked")   
  3. private static Map map = new HashMap() {{      
  4.     put("id""20090501");      
  5.     put("name""name1");   
  6.     put("age","20");   
  7. }};     
  8.   
  9. private static final Set<String> set = new HashSet<String>() {{      
  10.        add("lucy");      
  11.        add("lily");      
  12.        add("frank");      
  13.        add("bruce");      
  14. }};     
  15.   
  16. public static void printSet(Set set){   
  17.     Iterator it = set.iterator();   
  18.     while(it.hasNext()){   
  19.         System.out.println(it.next());   
  20.     }   
  21. }   
  22.   
  23. public static void printMap(Map map) {    
  24.        for (Object key : map.keySet()) {      
  25.            System.out.println(key + ":" + map.get(key));      
  26.        }     
  27. }   
  28.   
  29. public static void printSelf() {   
  30.     // 正则表达式“.”可以匹配任何单个的字符,要转义   
  31.     System.out.println(JavaDisabuse.class.getName().replaceAll("\\.""/")   
  32.             + ".class");   
  33.     // java.util.regex.Pattern.quote。它接受一个字符串作为参数,并可以添加必   
  34.     // 需的转义字符,它将返回一个正则表达式字符串   
  35.     System.out.println(JavaDisabuse.class.getName().replaceAll(   
  36.             Pattern.quote("."), "/")+ ".class");   
  37.        
  38.     //java.util.regex.Matcher.quoteReplacement,它将字   
  39.     //符串转换成相应的替代字符串。   
  40.     System.out.println(JavaDisabuse.class.getName().replaceAll("\\.",   
  41.             Matcher.quoteReplacement(File.separator)) + ".class");   
  42.        
  43.     System.out.println(JavaDisabuse.class.getName().   
  44.             replace('.', File.separatorChar) + ".class");   
  45. }   
  46.   
  47. /** ***********提防溢出*************** */  
  48. public static void longDivision() {   
  49.     final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;// 乘完后(已溢出)转型   
  50.     final long MICROS_PER_DAY_L = 24L * 60 * 60 * 1000 * 1000;   
  51.     final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;   
  52.   
  53.     System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);// 5   
  54.     System.out.println(MICROS_PER_DAY_L / MILLIS_PER_DAY);// 1000   
  55. }   
  56.   
  57. /** ***********执行精确小数运算********* */  
  58. public static BigDecimal subtract(String arg0, String arg1) {   
  59.     // 问题在于1.1 这个数字不能被精确表示成为一个double,因此它被表示成为最   
  60.     // 接近它的double值。   
  61.     System.out.println(2.00 - 1.10);// 二进制浮点数的double运算:0.8999999999999999   
  62.     return new BigDecimal(arg0).subtract(new BigDecimal(arg1));// 0.90   
  63. }   
  64.   
  65. /** *******求奇数(莫用i%2==1,负数失效)********* */  
  66. public static boolean isOdd(int i) {   
  67.     return i % 2 != 0;   
  68. }   
  69.   
  70. public static boolean isOdd1(int i) {   
  71.     return (i & 1) != 0;   
  72. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值