java 生成 5 位随机引脚

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33847225/
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-11-02 22:11:43  来源:igfitidea点击:

Generating a Random Pin of 5 Digits

java

提问by Craig

I would like to generate Unique Pins based on a random number I have found this on Stack Overflow How to generate a random five digit number Java. That Question uses time to generate the numbers so therefore you get a lot of duplicates.

我想根据我在 Stack Overflow How to generate a random 5 digit number Java上发现的随机数生成唯一 Pin 图。该问题使用时间来生成数字,因此您会得到很多重复项。

Here is my code

这是我的代码

public int createRandomPin() {
    int k = random.nextInt(Integer.SIZE);
    k = (k + 1) * 9999;
    if (9999 > k || k > 99999) {
        //then regenerate 
    } else {
        return k;
    }
}

My Question

我的问题

Java Compiler then gives a warning missing "return". As well I need to restructure the code so that if it isn't a 5 digit pin it generates again before it "returns k".

Java Compiler 然后给出一个缺少“return”的警告。同样,我需要重组代码,以便如果它不是 5 位引脚,它会在“返回 k”之前再次生成。

回答by Naruto

I would suggest to generate random number using SecureRandom class and then as it needs to be of 5 digits create a random number between 0 and 99999 using random.nextInt(100000) , here 0 is inclusive and 100000 is exclusive and then format it into 5 digit by appending zero.

我建议使用 SecureRandom 类生成随机数,然后因为它需要是 5 位数字,所以使用 random.nextInt(100000) 创建一个介于 0 和 99999 之间的随机数,这里 0 是包含的,100000 是不包括的,然后将其格式化为 5数字通过附加零。

SecureRandom random = new SecureRandom();
int num = random.nextInt(100000);
String formatted = String.format("%05d", num); 
System.out.println(formatted);

I hope this solves your problem

我希望这能解决你的问题

Edit: This post incorrectly said 10,000, has been edited to say 100,000

编辑:此贴错误地表示 10,000,已编辑为 100,000

回答by Hyman

Integer.SIZEyields the number in bits of the intdata type. This nothing has to do with the range span for a random value.

Integer.SIZE产生int数据类型的位数。这与随机值的范围跨度无关。

Using it as the argument of Random.nextIntdoesn't make any sense (actually it generates a random value in range [0,32)).

将它用作 of 的参数Random.nextInt没有任何意义(实际上它会在 range 中生成一个随机值[0,32))。

Just generate a int value = random.nextInt(100000)so that you will obtain a value in [0,99999].

只需生成一个 int 即可value = random.nextInt(100000)获得[0,99999].

Now your definition of 5 digits pin is not precise, 40could be interpreted as 00040so it's still 5 digits if you pad it. You must take this thing into account, since forcing 5 "visible" digits implies generating a number in range [10000,99999]but this is just an awkward solution.

现在您对 5 位数 pin 的定义不准确,40可以解释为00040,如果您填充它,它仍然是 5 位数。您必须考虑到这一点,因为强制 5 个“可见”数字意味着在范围内生成一个数字,[10000,99999]但这只是一个尴尬的解决方案。

回答by Rohit

RandomUtils.randomNumeric(int count)or RandomUtils.randomNumeric(int inLength, int maxLength)from apache common lang3 can also be used for the same purpose.

RandomUtils.randomNumeric(int count)或者RandomUtils.randomNumeric(int inLength, int maxLength)来自 apache 的 common lang3 也可以用于相同的目的。