Interesting usage of random numbers in java

It is easier to understand 

package com.wz.other;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
* Interesting features of random
*
* @author Administrator
* @create 2018-04 -23 8:42
*/
public class UseRandom {

public static void main(String[] args) {
// simple usage
// yields double precision floating point numbers between 0.0 and 10.0
double num = Math.random() * 10;
// Generate integers between 0 and 10
long num1 = Math.round(Math.random()*10);

// Advanced usage (create Random object every time)
Random random = new Random();
int num2 = random.nextInt(10);

// Concurrency scenario (jdk1.7), two advantages, single instance static access, as flexible as Math.random()
int num3 = ThreadLocalRandom.current().nextInt(10);

// Experience sharing
Math.round(Math.random() * 10); // Unbalanced distribution and rounding effect
Math.floor(Math.random( ) * 11); // The problem above can be solved

// In actual combat projects, do not use the first method below to get integers, but use the second method
Random rnd = new Random();
// The first method One method
System.out.println(Math.abs(rnd.nextInt()) % 10);
// The second method
System.out.println(rnd.nextInt(10));

}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324788928&siteId=291194637