Java的Math-BigInteger-Random类概述
1. Math类
Math类在java.lang包中.
常用的方法有
Math.abs(double x)返回x的绝对值.
Math.max(double x,double y)返回x和y的较大者.
Math.pow(double x,double y)返回x的y次幂.
Math.sqrt(double x)返回x的平方根.
Math.log(double x)返回x的自然对数.
Math.asin(double x)返回x的反正弦值.
Math.random()返回0-1之间的随机数(不包含端点).
例如
int x = (int)(Math.random()*b)+a可以获取[a,b]之间的随机整数.
2. BigInteger类
BigInteger类在java.math包中.
①创建一个BigInteger类对象的方法如下
BigInteger bigInteger = new BigInteger(Strings);
其中s为数字字符串,如果s中含有非数字字符,则会出现异常.
②常用方法如下
bigInteger为一个BigInteger类对象
a. 加
bigInteger.add(BigInteger bi)方法:bigInteger与bi相加.
b. 减
bigInteger.subtract(BigInteger bi)方法:bigInteger减去bi.
c. 乘
bigInteger.multiply(BigInteger bi)方法:bigInteger与bi相乘.
d. 除
bigInteger.divide(BigInteger bi)方法:bigInteger除以bi.
e. 取余
bigInteger.remainder(BigInteger bi)方法:bigInteger除以bi的余数.
f. 绝对值
bigInteger.abs()方法:bigInteger的绝对值.
g. 乘方
bigInteger.pow(int n)方法:bigInteger的n次方
h. 字符串bigInteger.toString(int p)方法:将bigInteger转化为p进制,p可以缺省,缺省值为10.
3. Random类
用法如下
①获取[a,b]之间随机整数
Random r = new Random();
int x = r.nextInt(b+a+1)+a;
如int x = r.nextInt(21)-10;可以获取[-10,10]之间的随机整数.
②随机获取true/false
boolean b = r.nextBoolean();即可获取一个随机布尔值.
Java的Math-BigInteger-Random类概述的更多相关文章
- Java基础知识强化82:Random类概述和方法使用
1. Random类 public class Random extends Object implements Serializable: 此类的实例用于生成伪随机数流.此类使用48位种子. (1) ...
- Java SE基础部分——常用类库之Math和Random类(随机产生数值)
//20160518 Math类常用方法 练习 package MyPackage; public class MathDemo {//定义主类和main方法 public static void m ...
- Java基础(三十五)Math、Random类和数字格式化(String.format方法)
一.Math类 Math类常用的方法: public static long abs (double a) 返回a的绝对值 public static double max (double a,dou ...
- 吴裕雄--天生自然java开发常用类库学习笔记:Math与Random类
public class MathDemo01{ public static void main(String args[]){ // Math类中的方法都是静态方法,直接使用“类.方法名称()”的形 ...
- Java基础3-数组操作;类概述
昨日内容回顾 数据类型 基本数据类型 1) byte, short, int, long, float, double 2) boolean[true, false] 3) char 100: 默认为 ...
- java基础之Random类
Random类 Random类中实现的随机算法是伪随机,也就是有规则的随机.在进行随机时,随机算法的起源数字称为种子数(seed), 在种子数的基础上进行一定的变换,从而产生需要的随机数字. 相同种子 ...
- Java API —— Random类
1.Random类概述 此类用于产生随机数 如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列. 2.构造 ...
- java Math和Random和UUID
Math类 public final class Math extends Object 以下X表示double,float,int, long abs(X x):求绝对值 max(X x1,X x2 ...
- 18、Random类简介
Random类概述 Random类在java.util包下,使用这个类可以生成随机数 package com.sutaoyu.usually_class; import java.util.Rando ...
- java随机函数使用方法Random
import java.util.Random; public class RandomNumber{ public static void main(String[] args) { // 使用ja ...
随机推荐
- 《学习OpenCV》课后习题解答9
题目:(P126) 创建一个程序,使其读入并显示一幅图像.当用户鼠标点击图像时,获取图像对应像素的颜色值(BGR),并在图像上点击鼠标处用文本将颜色值显示出来. 解答: 本题关键是会用cvGet2D获 ...
- HDU 2135 Rolling table
http://acm.hdu.edu.cn/showproblem.php?pid=2135 Problem Description After the 32nd ACM/ICPC regional ...
- HDU 1798 Tell me the area
http://acm.hdu.edu.cn/showproblem.php?pid=1798 Problem Description There are two circles in the ...
- 论 Web 前端加密的意义
论 Web 前端加密的意义 Web前端密码加密是否有意义? https://www.zhihu.com/question/25539382 https://blog.csdn.net/hla19910 ...
- DataBase -- Employees Earning More Than Their Managers My Submissions Question
Question: The Employee table holds all employees including their managers. Every employee has an Id, ...
- BZOJ day2_plus
大半夜的刷b站,好爽啊... 突破十九题 1008105110591088117911911192143218761951196821402242243824562463276128184720
- 【TMD模拟赛】黄金拼图 Cao
正解:Cao 据说这样的题是用来骗丛林生物上树的...... 这样的题除了考观察力之外还.........我们发现他异或了opt,恩,就这样,用离线推答案..... #include <cstd ...
- [BZOJ2090/2089] [Poi2010]Monotonicity 2/Monotonicity 树状数组优化dp
这个dp乍看不科学,仔细一看更不科学,所以作为一个执着BOY,我决定要造数据卡死波兰人民,但是我造着造着就......证出来了......... 这个就是把 < > =分开讨论每次找到f[ ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C (用map 超时)
C. Bear and Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- nodejs 喜欢报cannot find module .....的简单解决方案
在安装nodejs后使用命令npm install <package_name>一直喜欢报cannot find module........ 因为我之前在我的电脑上安装过nodejs,当 ...