package ne;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import static java.lang.Math.*;
public class c2 {
public static void main(String[] args) throws ParseException {
// System.out.printf("");
// int sun = 0;
// for (int i = 0; i < 100; i++) {
// sun += i;
// }
// System.out.printf("%d", sun);
// System.out.printf("%d", sun);
UUID id = UUID.randomUUID(); //随机ID
System.out.println(id); //dd8acc56-d02c-4188-b5f0-5238d16e46d5
System.out.println(id.toString().length()); //36
//随机类Random
Random rand = new Random();
//true false 真假随机
System.out.println(rand.nextBoolean());//false true
//0 - 5随机整数
System.out.println(rand.nextInt(6)); //2
System.out.println(Math.random()); //返回double 0.2185571904596617
//随机颜色
int rr = rand.nextInt(256);//0-255
int gg = rand.nextInt(256);
int bb = rand.nextInt(256);
System.out.printf("#%02x%02x%02x%n", rr, gg, bb); //#61bd99
System.out.println(rand.nextDouble()); //0.2169224401330715
System.out.println(rand.nextFloat()); //0.07115024
System.out.println(rand.nextLong()); //1927605436300879373
System.out.println(rand.nextInt()); //-177119358
System.out.println(rand.nextGaussian()); //-0.7582383393957878
//java新版权
var randX = new Random();
System.out.println(randX.nextInt(10, 21)); //产生一个范围内随机数
for (int i = 1; i <= 300; i++) {
System.out.printf("%d ", randX.nextInt(10, 21));
if (i % 20 == 0) {
System.out.println();
}
}
//数组随机获取
String[] stus = new String[]{"李四", "赵六", "张三", "王五", "李强", "jack", "james gosling"};
int size = stus.length;
System.out.println(size);
var randv = new Random();
int index = randv.nextInt(stus.length);
//index 5
String info = String.format("有%d人学生,本次选中的学生是:%s", size, stus[index]);
System.out.println(info);
//a-b 之间随机日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
var a = "2021-01-01 00:00:00";
var d1 = sdf.parse(a).getTime();
System.out.println(d1 / 1000);
//System.out.printf("%1$tF %1$tT%n",946656000000l);
var b = "2021-11-24 09:23:30";
var d2 = sdf.parse(b).getTime();
System.out.println(d2 / 1000);
//System.out.printf("%1$tF %1$tT%n",1451577599000L);
var r = new Random();
// 0 - num
var num = d2 - d1 + 1; //时间戳相减差值
var d3 = d1 + (long) (r.nextDouble() * num);
System.out.printf("%1$tF %1$tT%n", d3);
//2022-1-1 0:0:0 - now之间随机日期
System.out.printf("%1$tF %1$tT %n", 0l);
SimpleDateFormat sdfx = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (int i = 0; i <= 15; i++)
System.out.printf("%1$tF %1$tT %n", nextLong(sdfx.parse("2021-01-01 00:00:00").getTime(), new Date().getTime()));
//Math工具类 科学计算
//java.lang.Math 构造方法是私有,不让new
double pi = PI;
System.out.printf("======================\n");
System.out.println(pi);
System.out.println(Math.random());
System.out.println(round(1.4));//1 //四舍五入到最接近的 int值
System.out.println(round(1.5));//2
System.out.println(floor(1.4));//1.0 //小于或等于参数的最大(最接近正无穷大)浮点值,等于数学整数。
System.out.println(floor(1.5));//1.0
System.out.println(Math.ceil(1.4));//2.0 //最大(最接近负无穷大)的浮点值大于或等于参数,并且等于一个数学整数。
System.out.println(Math.ceil(1.5));//2.0
System.out.println(Math.pow(2, 3));
System.out.println(Math.max(10, 20));//20 //最大值
System.out.println(Math.min(10, 20));//10 //最小值
//System.out.println(Math.random());
//System.out.println(Math.random());
//System.out.println(Math.random());
//System.out.println(Math.random());
//产生0-9 随机数
for (int i = 1; i <= 20; i++) {
//0 - 9 随机整数
int numv = (int) round(Math.random() * (9 - 0)) + 0;
// 0.0000001 * 9 0.000009 0
// 0.999999924 *9 8.1 8
System.out.printf("%d ", numv);
}
//编写程序,输出2021-07-01 0:0:0 到 now 之间的随机Date
SimpleDateFormat sdfv = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdfv.parse("2021-7-1 0:0:0");
long start = d.getTime();
long now = System.currentTimeMillis();
long time = round(Math.random() * (now - start) + start);
System.out.println(sdfv.format(time));
//随机16进制颜色
int red = (int) round(Math.random() * 255);
int green = (int) round(Math.random() * 255);
int blue = (int) round(Math.random() * 255);
String color = String.format("#%02x%02x%02x", red, green, blue);
System.out.println(color);
//使用Math类,java lang下所有类系统会自动引
//1. import java.lang.Math,由于在java.lang包下会自动引入
System.out.println(PI);
//2. 正常使用一个对象需要实例化,Math官方构造方法私有,不允许使用new实例化
//可以直接使用类,调用静态方法(使用)
System.out.println(round(1.5));
String str = "ok";
System.out.println(PI);
System.out.println(floor(1.5));
System.out.println(ceil(1.2));
System.out.println(abs(-6));
System.out.println(pow(2, 3));
System.out.println(random());
System.out.println(max(10, 5));
// String d;
// int a = 20;
// int b = 30;
// int c = a > b ? a : b;
// System.out.println(c);
// Color cc = new Color(0xba, 0x50, 0x71);
// int red = (int) round(Math.random() * 255);
// int green = (int) round(Math.random() * 255);
// int blue = (int) round(Math.random() * 255);
// String color = String.format("#%02x%02x%02x", red, green, blue);
// System.out.println(color);
}
/**
* 静态方法,用于生成指定范围的long
*
* @param a
* @param b
* @return
*/
public static long nextLong(long a, long b) {
var rand = new Random();
return (long) (rand.nextDouble() * (b - a + 1) + a);
}
JAVA 随机类 Math工具类
于 2023-10-14 09:38:53 首次发布