大家好,欢迎来到IT知识分享网。
方法一:Math.random()
Math.random() 方法返回的是一个介于 0.0(包含)和 1.0(不包含)之间的随机浮点数。
1、生成0.0到1.0之间的浮点数:double f= Math.random();
2、生成0.0到50.0之间的双精度浮点数:double randomDouble = Math.random() * 50;
3、生成大写字母:char ch=(char)(65 + (Math.random() * 26))
4、生成小写字母:char ch=(char)(92 + (Math.random() * 26))
5、生成0-9的整数:int n = (int) (Math.random() * 10);
6. 生成1到100之间的整数:
public class IntegerExample { public static void main(String[] args) { int n = (int) (Math.random() * 100) + 1; // 生成1到100之间的随机整数 System.out.println("随机范围整数: " + n); } }
方法二:使用 java.util.Random
1. 生成随机整数
import java.util.Random; public class RandomIntegerExample { public static void main(String[] args) { Random random = new Random(); int n = random.nextInt(100); // 生成0到99之间的随机整数 System.out.println("随机整数: " + n); } }
2. 生成随机浮点数
import java.util.Random; public class RandomFloatExample { public static void main(String[] args) { Random random = new Random(); float f = random.nextFloat(); // 生成0.0到1.0之间的随机浮点数 System.out.println("随机浮点数: " + f); } }
3. 生成随机布尔值
import java.util.Random; public class RandomBooleanExample { public static void main(String[] args) { Random random = new Random(); boolean b = random.nextBoolean(); // 生成随机布尔值 System.out.println("随机布尔值: " + b); } }
4. 生成随机字节数组
import java.util.Random; public class RandomByteArrayExample { public static void main(String[] args) { Random random = new Random(); byte[] byteArray = new byte[10]; // 指定数组长度为10 random.nextBytes(byteArray); // 填充随机字节 System.out.println("随机字节数组: " + java.util.Arrays.toString(byteArray)); } }
5. 生成随机双精度浮点数
生成一个随机的双精度浮点数,范围为 0.0(包含)到 1.0(不包含)。
import java.util.Random; public class RandomDouble { public static void main(String[] args) { Random random = new Random(); double d= random.nextDouble(); // 生成0.0到1.0之间的随机双精度浮点数 System.out.println("随机双精度浮点数: " + d); } }
方法三:使用 ThreadLocalRandom
ThreadLocalRandom 类适用于多线程环境,能够提供更好的性能。
1、生成0到9之间的随机整数:
int randomInt = ThreadLocalRandom.current().nextInt(10);
2、 生成0.0到1.0之间的随机浮点数
double randomFloat = ThreadLocalRandom.current().nextDouble();
3、 生成1到100之间的随机整数
int randomInt = ThreadLocalRandom.current().nextInt(1, 101);
4、生成0.0到50.0之间的随机双精度浮点数
double randomDouble = ThreadLocalRandom.current().nextDouble(0.0, 50.0);
5、模拟随机选择一个元素(从数组中选择)
import java.util.concurrent.ThreadLocalRandom; public class Example { public static void main(String[] args) { String[] items = {"苹果", "香蕉", "橙子", "梨", "桃"}; int n = ThreadLocalRandom.current().nextInt(items.length); // 随机索引 System.out.println("随机选择的水果: " + items[n]); } }
方法四: 使用System.currentTimeMillis();
currentTimeMillis()方法可以返回一个从1970年1月1号0点0分0秒到目前的一个毫秒数,返回类型是long,将其对一些数取模,就可以把数字限制在一个范围内
public class RandomFromMillis { public static void main(String[] args) { long c = System.currentTimeMillis(); // 获取当前时间的毫秒数 int n = (int)(c % 100); // 生成0到99之间的随机整数 System.out.println(n); } }
例题
题目1:生成一个4个字符随机验证码,由大写、小写字母和数字组成
方法一:定义一个字符串存储所有数字和字储
public class yanzhengma { public static void main(String[] args) { System.out.print("字符随机验证码为:"); String str="0qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"; int len=str.length(); int n=0; for (int i = 0; i < 4; i++) { n=(int)(Math.random()*len); System.out.print(str.charAt(n)); } } }
方法二:先生成字母的ASCII码,再将十进制转换成字符
import java.util.Random; public class homework4 { public static void main(String[] args) { String generatedString = generateRandomString(4); System.out.println("随机生成的字符: " + generatedString); } public static String generateRandomString(int length) { Random random = new Random(); StringBuilder result = new StringBuilder(length); for (int i = 0; i < length; i++) { int n = random.nextInt(3); // 生成0-2的随机数 if (n == 0) { // 生成0-9的数字 result.append(random.nextInt(10)); } else if (n == 1) { // 生成小写字母 char lower = (char) ('a' + random.nextInt(26)); result.append(lower); } else { // 生成大写字母 char upper = (char) ('A' + random.nextInt(26)); result.append(upper); } } return result.toString(); } }
题目2:生成一个车牌号,车牌号由三个大写字母和四个数字组成
方法一:定义一个字符串存储所有字母
import java.util.Random; public class exercises_4_25 { public static void main(String[] args) { Random random = new Random(); String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; StringBuilder plate = new StringBuilder(); // 生成三个大写字母 for (int i = 0; i < 3; i++) { char letter = letters.charAt(random.nextInt(letters.length())); plate.append(letter); } // 生成四个数字 for (int i = 0; i < 4; i++) { int digit = random.nextInt(10); plate.append(digit); } System.out.println("车牌号为: " + plate.toString()); } }
方法二:先生成大写字母的ASCII码,再将十进制转换成字符
public class exercises_4_25 { public static void letter() { int i=65+(int)(Math.random()*25); //随机得到65——95的整数 char ch=(char)i; System.out.print(ch); } public static void number() { int n=(int)(Math.random()*10); System.out.print(n); } public static void main(String[] args) { System.out.println("车牌号为:"); for (int i = 0; i <3 ; i++) { letter(); } for (int i = 0; i <4 ; i++) { number(); } } }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/114170.html