Java 生成 6 位随机数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51322750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:04:12  来源:igfitidea点击:

Generate 6 digit random number

java

提问by Kraken

I just want to generate 6 digit random number, and the range should be start from 000000 to 999999.

我只想生成 6 位随机数,范围应该从 000000 到 999999。

new Random().nextInt(999999)is returning me number but it is not in 6 digit.

new Random().nextInt(999999)正在返回我的号码,但它不是 6 位数字。

采纳答案by Dev Sabby

Its as simple as that, you can use your code and just do one thing extra here

就这么简单,你可以使用你的代码,在这里只做一件事

String.format("%06d", number);

this will return your number in string format, so the "0" will be "000000".

这将以字符串格式返回您的号码,因此“0”将是“000000”。

Here is the code.

这是代码。

public static String getRandomNumberString() {
    // It will generate 6 digit random Number.
    // from 0 to 999999
    Random rnd = new Random();
    int number = rnd.nextInt(999999);

    // this will convert any number sequence into 6 character.
    return String.format("%06d", number);
}

回答by Karol Dowbecki

If you need a six digit number it has to start at 100000

如果你需要一个六位数的号码,它必须从 100000

int i = new Random().nextInt(900000) + 100000;

Leading zeros do not have effect, 000000is the same as 0. You can further simplify it with ThreadLocalRandomif you are on Java 7+:

前导零没有作用,0000000. ThreadLocalRandom如果您使用的是 Java 7+,您可以进一步简化它:

int i = ThreadLocalRandom.current().nextInt(100000, 1000000)

回答by Codebender

1 + nextInt(2) shall always give 1 or 2. You then multiply it by 10000 to satisfy your requirement and then add a number between [0..9999].

1 + nextInt(2) 应始终给出 1 或 2。然后将其乘以 10000 以满足您的要求,然后在 [0..9999] 之间添加一个数字。

already solved here

已经在这里解决

public int gen() 
{ 
    Random r = new Random( System.currentTimeMillis() );
    return ((1 + r.nextInt(2)) * 10000 + r.nextInt(10000)); 
}

回答by Bonfra

i know it's very difficult but you can do something like this: create a class for BinaryNumber; create a constructor that generate a char[] of 6 character where every single one is generated with a randomiser from 0 to 1 override the toStrig() method so that it will return the digits char[] as a string if you want to display it. then crate a method toInt() that esaminate the string char by char with a for and turn it in a decimal base number by multiplying current digit to 10 to the pow of i:

我知道这很困难,但您可以这样做:为 BinaryNumber 创建一个类;创建一个生成 6 个字符的 char[] 的构造函数,其中每个字符都使用从 0 到 1 的随机生成器生成,覆盖 toSrig() 方法,以便如果要显示它,它将以字符串形式返回数字 char[] . 然后创建一个 toInt() 方法,该方法使用 for 逐个字符化字符串,并通过将当前数字乘以 10 到 i 的 pow 将其转换为十进制基数:

char[] digits = {‘1' , ‘0' , ‘1' , ‘1' , ‘0' , ‘1'};
//random 

int result = 0;
for( int i = 0; i < digits.length; i++) {
    result += Integer.parseInt(digits[i]) * Math.pow(10, i);
}

return result;

回答by Abhishek Tiwari

This is the code in java which generate a 6 digit random code.

这是java中生成6位随机代码的代码。

import java.util.*;
public class HelloWorld{

     public static void main(String []args)
     {

        Random r=new Random();
                        HashSet<Integer> set= new HashSet<Integer>();
                        while(set.size()<1)
                        {
                            int ran=r.nextInt(99)+100000;
                            set.add(ran);
                        }
                        int len = 6;
                        String random=String.valueOf(len);
                        for(int  random1:set)
                        {
                            System.out.println(random1);
                            random=Integer.toString(random1);

                        }
     }
}