随机生成6位数、随机生成不重复的6位数

随机生成一个几位数,这种比较常见的操作今天我们来看一下,例如随机生成6位数,直接来简单明了的吧:

int num = (int) ((Math.random() * 9 + 1) * 100000);

最终num就是需要的6位随机数。

同理要是想得到随机的五位数和七位数呢?

//随机的五位数
int num = (int) ((Math.random() * 9 + 1) * 10000);
//随机的七位数
int num = (int) ((Math.random() * 9 + 1) * 1000000);

以此类推想得到随机的n位数都可以,只需要改动后面的100000就好。

如果想生成随机的六位数,并且每位数都不重复:

//随机生成六位数,并且每位数都不重复
  public static int Num() {
        int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        Random rand = new Random();
        for (int i = 10; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = array[index];
            array[index] = array[i - 1];
            array[i - 1] = tmp;
        }
        int result = 0;
        for (int i = 0; i < 6; i++) {
            result = result * 10 + array[i];
        }
        if (String.valueOf(result).length() == 6) {
            return result;
        } else {
            return Num();
        }
    }

以上就是随机生成的六位数,并且可以达到每位数都不重复。

····························································分割性·····························································

针对有技术朋友说可能会生成首位是0的数字,这样生成的随机数就变成了五位数,在此我做一下论证:

首先Math.random()的意思是得到一个范围在0-1之间的double类型的随机小数(例如:0.52015784514,也有可能是0.00597308946201);

Math.random()的取值是0-1之间的随机小数(事实上取不到0和1);

Math.random() * 9表示乘以9以后是0-9之间的随机小数,也就是0.****到8.****之间的小数(大于0而小于9);

(Math.random() * 9 + 1)表示:"+1"之后就会得到1-9之间的随机小数,也就是1.******到9.*******之间的小数;

((Math.random() * 9 + 1) * 100000)表示:乘以100000后,这个随机小数的小数点就会往后移六位,得到1*****.*****到9*****.******之间的数字(现在小数点前是六位数);

最后(int) ((Math.random() * 9 + 1) * 100000)表示:经过int类型转换之后,得到了首位数一定不是0的随机六位数

还可以通过以下代码运行之后打印结果看一下:

        //生成一千个随机六位数
        for (int i = 0; i < 5000; i++) {
            int num = (int) ((Math.random() * 9 + 1) * 100000);
            System.out.println("随机生成的六位数:" + num);
            String num_str = String.valueOf(num);
            //验证是否会生成五位数,或者首位是0的数
            if (num_str.length() == 5) {
                System.out.println("生成的五位数:" + num);
            }
        }


以上代码是生成了5000个随机的六位数,以下是结果:
 

  • 8
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 要让Java生成随机的5位数重复,可以采用以下的方法: 1. 创建一个空的数组,用来存放生成的随机数。 2. 使用Java的Random类,通过nextint方法生成一个5位数(10000-99999之间的数字,不包括99999)。 3. 检查生成的随机数是否在数组中已存在,如果已存在,则重新生成随机数,直到得到一个不重复的数字为止。 4. 将生成的随机数存储到数组中,并继续生成下一个数字,直到数组中有5个不重复的数字为止。 5. 返回生成的随机数数组。 在代码实现上,可以通过一个while循环来不断生成随机数,直到满足条件为止。具体实现可以参考以下代码片段: ``` import java.util.Arrays; import java.util.Random; public class RandomNumberGenerator { public static void main(String[] args) { int[] randomNumbers = new int[5]; Random random = new Random(); int count = 0; while (count < 5) { int number = random.nextInt(90000) + 10000; if (!contains(randomNumbers, number)) { randomNumbers[count] = number; count++; } } System.out.println(Arrays.toString(randomNumbers)); } private static boolean contains(int[] arr, int target) { for (int num : arr) { if (num == target) { return true; } } return false; } } ``` 这段代码使用了一个长度为5的int数组来存储生成的随机数,通过一个计数器count来统计已经生成的随机数的个数。在while循环中,使用random.nextInt方法生成随机数,再调用contains方法检查是否已经存在于数组中。如果不存在,则将该数字存入数组,并将计数器加一。最终输出数组即为5个不重复随机数字。 ### 回答2: Java是一种面向对象的编程语言,具有高效性、可移植性和可靠性等优势。当需要在Java中生成一组5位数重复随机数时,可以采用以下方法: 1. 定义一个长度为5的数组arr用于存放随机数。 2. 使用Random类创建一个Random对象random,调用nextInt()方法生成随机数。 3. 判断随机数是否在数组arr中已经存在,若存在则重新生成随机数。 4. 若随机数不存在于数组中,则将其存入数组中。 5. 当数组中元素达到5个时,退出循环。 6. 最终输出数组中的所有元素。 以下为示例代码: ``` public static void main(String[] args) { int[] arr = new int[5]; //定义长度为5的数组 int index = 0; Random random = new Random(); while (index < 5) { //循环生成随机数 int num = random.nextInt(100000); //生成0-99999之间的随机数 boolean flag = false; //标记是否已在数组中存在 for (int i = 0; i < index; i++) { if (arr[i] == num) { //如果已存在,则重新生成随机数 flag = true; break; } } if (!flag) { //如果不存在,则存入数组 arr[index] = num; index++; } } System.out.println("随机数为:"); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } ``` 上述代码中,定义了一个长度为5的数组arr用于存放随机数,使用Random类创建了一个Random对象random,并调用nextInt()方法生成随机数。随后使用for循环判断随机数是否已经存在于数组中,若存在则重新生成随机数,并将其存入数组中。最终通过循环输出数组中的所有元素。 通过以上方法,我们可以在Java中生成一组5位数重复随机数,希望可以帮助到您。 ### 回答3: 要生成不重复的五位数,可以利用Java提供的Random类和HashSet数据结构来实现。 首先,我们可以使用Random类生成五位数随机数,代码如下: ``` Random random = new Random(); int n = random.nextInt(90000) + 10000; ``` 这段代码生成一个在10000到99999之间的随机整数n。 接着,我们可以创建一个HashSet数据结构,并将生成的随机数加入到其中。HashSet是一种无序、不重复的集合,当我们往其中添加元素时,如果已经存在相同的元素,那么添加操作就会失败。这正好符合我们需要生成不重复数字的条件。 代码如下: ``` Set<Integer> set = new HashSet<>(); while(set.size() < 5){ Random random = new Random(); int n = random.nextInt(90000) + 10000; set.add(n); } ``` 这段代码创建了一个HashSet集合set,并使用while循环加入元素,当集合大小为5时停止循环。 最后,我们可以将集合中的元素取出,输出到控制台。 代码如下: ``` for(int num : set){ System.out.print(num + " "); } ``` 这段代码使用for循环遍历HashSet集合set,将其中的元素取出,赋值给变量num,并输出到控制台。 完整代码如下: ``` import java.util.HashSet; import java.util.Random; import java.util.Set; public class Main { public static void main(String[] args) { Set<Integer> set = new HashSet<>(); while(set.size() < 5){ Random random = new Random(); int n = random.nextInt(90000) + 10000; set.add(n); } for(int num : set){ System.out.print(num + " "); } } } ``` 执行上述代码,可以得到如下结果: ``` 67413 24073 23843 89754 69010 ``` 这就是随机生成的五个不重复的五位数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值