【Java工具类】----产生随机数

xiaoxiao2021-02-28  97

/** * @Title: randomUtil.java * @Package org.csun.nc.util * @Description: TODO * @author chisj chisj@foxmail.com * @date 2017年5月7日 */ package org.csun.nc.util; import java.util.Random; /** * ClassName: randomUtil * @Description: 随机数工具类 * @author chisj chisj@foxmail.com * @date 2017年5月7日 */ public class randomUtil { //方法1 public static int nextInt1(final int min, final int max) { Random rand= new Random(); int tmp = Math.abs(rand.nextInt()); return (tmp % (max - min + 1) + min); } //方法2:不会生成小于100000的数 public static int nextInt2() { Random random = new Random(); int x = random.nextInt(899999); //int x = x+100000; return x; } //方法3 public static int nextInt3() { int n = 0 ; while (n < 100000) { n = (int)(Math.random()*1000000); } //System.out.println(n); return n; } //方法4 public static int nextInt4() { String str = ""; str += (int)(Math.random()*9+1); for(int i = 0; i < 5; i++){ str += (int)(Math.random()*10); } int num = Integer.parseInt(str); //System.out.println(num); return num; } //方法5 //System.out.println((int)((Math.random()*9+1)*100000)); //方法6 public static String nextInt6() { Random random = new Random(); String result=""; for(int i=0;i<6;i++){ result+=random.nextInt(10); } //System.out.print(result); return result; } }
转载请注明原文地址: https://www.6miu.com/read-80353.html

最新回复(0)