Math.random、Random随机数的生成
Math.random() 方法
Math.random() 方法获取的数据区间为 [0.0 , 1.0) double 型随机数,即 0.0 <= Math.random() < 1.0;
案例:生成随机整数
使用方法:(数据类型)(最小值 + Math.random() ) * (最大值 - 最小值 + 1);// 随机数范围包含最小值和最大值
public class MathRandom {
public static void main(String[] args) {
// 生成 -99 到 99 的整数
System.out.println("生成任意一个 -99 ~ 99 的整数:\t" + GetRandomNum(-99 , 99));
}
public static int GetRandomNum(int num1, int num2){ // 生成 -99 99之间的随机数
int res = (int)(num1 + Math.random() * (num2 - num1 + 1));
return res;
}
}
案例:生成随机字符
public class MathRandom {
public static void main(String[] args) {
// 获取a~z之间的随机字符
System.out.println("任意一个小写字符: " + GetRandomChar('a', 'z'));
// 获取A~Z之间的随机字符
System.out.println("任意一个大写字符: " + GetRandomChar('A', 'Z'));
// 获取0~9之间的随机字符
System.out.println("0到9任意一个数字字符: " + GetRandomChar('0', '9'));
}
public static char GetRandomChar(char char1, char char2){
char res = (char)(char1 + Math.random() * (char2 - char1 + 1));
return res;
}
}
Random()类
使用步骤
1、导包
import java.util.Random;
2、创建对象
Random r = new Random();
3、获取随机数
//Random获取的数据区间为[0,1)
int num = r.nextInt(10); //获取数据区间为[0,10)内的整数
4、Random中提供了获取各种类型数据的方法
方法 | 说明 |
---|---|
public int nextInt() | 返回一个随机整数。 |
public int nextInt(itn) | 返回大于等于0且小于n的随机整数。 |
public long nextLong() | 返回一个随机长整型值。 |
public boolean nextBoolean() | 返回一个随机布尔型值。 |
public float nextFloat() | 返回一个随机浮点型值。 |
public double nextDouble() | 返回一个随机双精度型值。 |
public double nextGaussian() | 返回一个概率密度为高斯分布的双精度值。 |
Random生产区间随机数的技巧:减加法
import java.util.Random; // 导包
public class RandomTest {
public static void main(String[] args) {
// 创建随机数对象
Random r = new Random();
//生成 10 - 25 的随机数 ==> (0 - 15) + 10
int data = r.nextInt(16) + 10;
System.out.println(data);
//生成 62 - 81 的随机数 ==> (0 - 19) + 62
int data1 = r.nextInt(20) + 62;
System.out.println(data1);
}
案例:猜测随机一个幸运号码 1 - 100
需求: 随机生产一个 1 - 100 之间的数据,提示用户猜测,猜大提示过大,猜小提示过小。
分析:
1)随机生产一个 1 - 100 之间的数据;
2)使用死循环让用户不断猜测并提示用户,猜大提示过大,猜小提示过小。
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 1、随机幸运号码 1 - 100之间 (0 - 99) + 1
Random r = new Random();
int luckNumber = r.nextInt(100) + 1;
// 2、使用一个死循环让用户不断的去猜测,并给出提示
Scanner sc = new Scanner(System.in);
while (true){
// 让用户输入数据
System.out.println("请输入您猜测的数据(1 - 100)");
int guessNumber = sc.nextInt();
// 3、判断这个猜测的号码与幸运号码的大小情况
if(guessNumber > luckNumber){
System.out.println("您猜测的数据过大");
}else if (luckNumber < guessNumber){
System.out.println("您猜测的数据过小");
}else {
System.out.println("恭喜您,猜中了!");
break; // 直接跳出并结束当前死循环!
}
}
}
}
案例:生成随机字符
import java.util.Random;
public class RandomString {
public static void main(String[] args)
{
System.out.println("随机生成6位数的字符串: " + getRandomString(6));
System.out.println("随机生成8位数的字符串: " + getRandomString(8));
System.out.println("随机生成10位数的字符串: " + getRandomString(10));
}
// 随机生成字符串
public static String getRandomString(int length)
{
// 生成字符范围
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
// StringBuffer() 构造一个不带字符的字符串缓冲区,其初始容量为16个字符
StringBuffer str = new StringBuffer();
for(int i=0; i< length; i++)
{
// 从 base.length 中获得一个随机数得到字符下标
int number = random.nextInt(base.length());
// append() 方法相当于 " + ",将指定的字符串追加到此字符序列
// charAt() 方法 返回指定索引处的字符
str.append(base.charAt(number));
}
// toString()就是把非字符串的数据类型转化为字符串
return str.toString();
}
}