Three generation methods:
1. Get a long type of current time milliseconds through System.currentTimeMillis() number.
2. Return a double value between 0 and 1 through Math.random().
3. Generate a random number through the Random class. This is a professional Random tool class with powerful functions.
First type:
Get random numbers through System.currentTimeMillis(). It actually gets the current time in milliseconds, which is of type long. The usage method is as follows:
final long l = System.currentTimeMillis();
(Free video tutorial sharing: java video tutorial)
If you want to get an integer of type int, you only need to convert the above result to int Just type. For example, get an integer between [0, 100). The method is as follows:
final long l = System.currentTimeMillis(); final int i = (int)( l % 100 );
Second:
Get random numbers through Math.random(). In fact, it returns a double value between 0 (inclusive) and 1 (exclusive). The usage method is as follows:
final double d = Math.random();
To obtain an integer of type int, you only need to convert the above result to type int. For example, get an integer between [0, 100). The method is as follows:
final double d = Math.random(); final int i = (int)(d*100);
The third method:
Get random numbers through the Random class. The usage method is as follows:
1. Create a Random object. There are two methods to create Random objects, as follows:
Random random = new Random();//默认构造方法 Random random = new Random(1000);//指定种子数字
(02) Get random numbers through Random objects. Random value types supported by Random include: boolean, byte, int, long, float, double.
For example, get the int integer between [0, 100). The method is as follows:
int i2 = random.nextInt(100);
Code example:
1 import java.util.Random; 2 import java.lang.Math; 3 4 /** 5 * java 的随机数测试程序。共3种获取随机数的方法: 6 * (01)、通过System.currentTimeMillis()来获取一个当前时间毫秒数的long型数字。 7 * (02)、通过Math.random()返回一个0到1之间的double值。 8 * (03)、通过Random类来产生一个随机数,这个是专业的Random工具类,功能强大。 9 * 10 * @author skywang 11 * @email kuiwu-wang@163.com 12 */ 13 public class RandomTest{ 14 15 public static void main(String args[]){ 16 17 // 通过System的currentTimeMillis()返回随机数 18 testSystemTimeMillis(); 19 20 // 通过Math的random()返回随机数 21 testMathRandom(); 22 23 // 新建“种子为1000”的Random对象,并通过该种子去测试Random的API 24 testRandomAPIs(new Random(1000), " 1st Random(1000)"); 25 testRandomAPIs(new Random(1000), " 2nd Random(1000)"); 26 // 新建“默认种子”的Random对象,并通过该种子去测试Random的API 27 testRandomAPIs(new Random(), " 1st Random()"); 28 testRandomAPIs(new Random(), " 2nd Random()"); 29 } 30 31 /** 32 * 返回随机数-01:测试System的currentTimeMillis() 33 */ 34 private static void testSystemTimeMillis() { 35 // 通过 36 final long l = System.currentTimeMillis(); 37 // 通过l获取一个[0, 100)之间的整数 38 final int i = (int)( l % 100 ); 39 40 System.out.printf("\n---- System.currentTimeMillis() ----\n l=%s i=%s\n", l, i); 41 } 42 43 44 /** 45 * 返回随机数-02:测试Math的random() 46 */ 47 private static void testMathRandom() { 48 // 通过Math的random()函数返回一个double类型随机数,范围[0.0, 1.0) 49 final double d = Math.random(); 50 // 通过d获取一个[0, 100)之间的整数 51 final int i = (int)(d*100); 52 53 System.out.printf("\n---- Math.random() ----\n d=%s i=%s\n", d, i); 54 } 55 56 57 /** 58 * 返回随机数-03:测试Random的API 59 */ 60 private static void testRandomAPIs(Random random, String title) { 61 final int BUFFER_LEN = 5; 62 63 // 获取随机的boolean值 64 boolean b = random.nextBoolean(); 65 // 获取随机的数组buf[] 66 byte[] buf = new byte[BUFFER_LEN]; 67 random.nextBytes(buf); 68 // 获取随机的Double值,范围[0.0, 1.0) 69 double d = random.nextDouble(); 70 // 获取随机的float值,范围[0.0, 1.0) 71 float f = random.nextFloat(); 72 // 获取随机的int值 73 int i1 = random.nextInt(); 74 // 获取随机的[0,100)之间的int值 75 int i2 = random.nextInt(100); 76 // 获取随机的高斯分布的double值 77 double g = random.nextGaussian(); 78 // 获取随机的long值 79 long l = random.nextLong(); 80 81 System.out.printf("\n---- %s ----\nb=%s, d=%s, f=%s, i1=%s, i2=%s, g=%s, l=%s, buf=[", 82 title, b, d, f, i1, i2, g, l); 83 for (byte bt:buf) 84 System.out.printf("%s, ", bt); 85 System.out.println("]"); 86 } 87 }
Recommended related articles and tutorials: java introductory tutorial
The above is the detailed content of How to get different random numbers in java. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
