zoukankan      html  css  js  c++  java
  • java Math类

    原文  http://blog.csdn.net/chichoxian/article/details/20923501

    Java中比较常用的几个数学公式的总结:

    //取整,返回小于目标函数的最大整数,如下将会返回-2
    Math.floor(-1.8);
    //取整,返回发育目标数的最小整数
    Math.ceil()
    //四舍五入取整
    Math.round()
    //计算平方根
    Math.sqrt()
    //计算立方根
    Math.cbrt()
    //返回欧拉数e的n次幂
    Math.exp(3);
    //计算乘方,下面是计算3的2次方
    Math.pow(3,2);
    //计算自然对数
    Math.log();
    //计算绝对值
    Math.abs();
    //计算最大值
    Math.max(2.3,4.5);
    //计算最小值
    Math.min(,);
    //返回一个伪随机数,该数大于等于0.0并且小于1.0
    Math.random

    Random类专门用于生成一个伪随机数,它有两个构造器:一个构造器使用默认的种子(以当前时间作为种子),另一个构造器需要程序员显示的传入一个long型整数的种子。

    Random比Math的random()方法提供了更多的方式来生成各种伪随机数。

    e.g

    import java.util.Arrays;
    import java.util.Random;
    
    public class RandomTest {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    	// TODO Auto-generated method stub
    	Random rand = new Random();
    	System.out.println("随机布尔数" + rand.nextBoolean());
    	byte[] buffer = new byte[16];
    	rand.nextBytes(buffer);
    	//生产一个含有16个数组元素的随机数数组
    	System.out.println(Arrays.toString(buffer));
    	
    	System.out.println("rand.nextDouble()" + rand.nextDouble());
    	
    	System.out.println("Float浮点数" + rand.nextFloat());
    	
    	System.out.println("rand.nextGaussian" + rand.nextGaussian());
    	
    	System.out.println("" + rand.nextInt());
    	
    	//生产一个0~32之间的随机整数
    	System.out.println("rand.nextInt(32)" + rand.nextInt(32));
    	
    	System.out.println("rand.nextLong" + rand.nextLong());
        }
    
    }

    为了避免两个Random对象产生相同的数字序列,通常推荐使用当前时间作为Random对象的种子,代码如下:

    Random  rand = new Random(System.currentTimeMillis());

    在 java7 中引入了ThreadLocalRandom 

    在多线程的情况下使用ThreadLocalRandom的方式与使用Random基本类似,如下程序·片段示范了ThreadLocalRandom的用法:

    首先使用current()产生随机序列之后使用nextCXxx()来产生想要的伪随机序列 :

    ThreadLocalRandom trand= ThreadLocalRandom.current();
            
            int val = rand.nextInt(4,64);

    产生4~64之间的伪随机数 

    public class MathDemo {  
        public static void main(String args[]){  
            /** 
             * abs求绝对值 
             */  
            System.out.println(Math.abs(-10.4));    //10.4  
            System.out.println(Math.abs(10.1));     //10.1  
              
            /** 
             * ceil天花板的意思,就是返回大的值,注意一些特殊值 
             */  
            System.out.println(Math.ceil(-10.1));   //-10.0  
            System.out.println(Math.ceil(10.7));    //11.0  
            System.out.println(Math.ceil(-0.7));    //-0.0  
            System.out.println(Math.ceil(0.0));     //0.0  
            System.out.println(Math.ceil(-0.0));    //-0.0  
              
            /** 
             * floor地板的意思,就是返回小的值 
             */  
            System.out.println(Math.floor(-10.1));  //-11.0  
            System.out.println(Math.floor(10.7));   //10.0  
            System.out.println(Math.floor(-0.7));   //-1.0  
            System.out.println(Math.floor(0.0));    //0.0  
            System.out.println(Math.floor(-0.0));   //-0.0  
              
            /** 
             * max 两个中返回大的值,min和它相反,就不举例了 
             */  
            System.out.println(Math.max(-10.1, -10));   //-10.0  
            System.out.println(Math.max(10.7, 10));     //10.7  
            System.out.println(Math.max(0.0, -0.0));    //0.0  
              
            /** 
             * random 取得一个大于或者等于0.0小于不等于1.0的随机数 
             */  
            System.out.println(Math.random());  //0.08417657924317234  
            System.out.println(Math.random());  //0.43527904004403717  
              
            /** 
             * rint 四舍五入,返回double值 
             * 注意.5的时候会取偶数 
             */  
            System.out.println(Math.rint(10.1));    //10.0  
            System.out.println(Math.rint(10.7));    //11.0  
            System.out.println(Math.rint(11.5));    //12.0  
            System.out.println(Math.rint(10.5));    //10.0  
            System.out.println(Math.rint(10.51));   //11.0  
            System.out.println(Math.rint(-10.5));   //-10.0  
            System.out.println(Math.rint(-11.5));   //-12.0  
            System.out.println(Math.rint(-10.51));  //-11.0  
            System.out.println(Math.rint(-10.6));   //-11.0  
            System.out.println(Math.rint(-10.2));   //-10.0  
              
            /** 
             * round 四舍五入,float时返回int值,double时返回long值 
             */  
            System.out.println(Math.round(10.1));   //10  
            System.out.println(Math.round(10.7));   //11  
            System.out.println(Math.round(10.5));   //11  
            System.out.println(Math.round(10.51));  //11  
            System.out.println(Math.round(-10.5));  //-10  
            System.out.println(Math.round(-10.51)); //-11  
            System.out.println(Math.round(-10.6));  //-11  
            System.out.println(Math.round(-10.2));  //-10  
        }  
    }  
  • 相关阅读:
    【C++11】准备:gcc 4.9.0编译安装
    【转】C++ 虚函数表解析
    【工作总结】内存泄漏总结
    【工作总结】C++ string工具类
    工作总结
    【工作笔记】CxImage简介
    【工作笔记】没有界面程序调用ActiveX控件
    【工作笔记】npapi插件编写
    【工作笔记】VLC播放器在chrome中的调用
    Linux多任务编程之三:exec函数族及其基础实验(转)
  • 原文地址:https://www.cnblogs.com/zhaoyang-1989/p/6722427.html
Copyright © 2011-2022 走看看