简体   繁体   English

我如何生成一个没有重复数字的 4 位 pin

[英]How do i generate a 4-digit pin with no duplicating number

I'm new to android programming and i want to make a 4 Digit PIN generator without any repeats.我是 android 编程的新手,我想制作一个没有任何重复的 4 位 PIN 生成器。 How do I do it??我该怎么做?? I don't know how to loop that very well yet.我还不知道如何很好地循环。 Thank you!!谢谢!!

I already tried Random but it gives me numbers that repeat.我已经尝试过 Random 但它给了我重复的数字。

int randomPIN = (int)(Math.random()*9000)+1000;

String pin = String.valueOf(randomPIN);
dummy.setText(pin);

I'm looking for an outcome of 1354, 4682, 3645 but the results are mostly 3344, 6577, 1988我正在寻找 1354、4682、3645 的结果,但结果大多是 3344、6577、1988

Create a list of the digits, shuffle it, and return the first four digits.创建一个数字列表,将其打乱,然后返回前四位数字。 Here's one way of doing it as a static method:这是将其作为静态方法的一种方法:

/* No need for a new list each time */
private static final List<Integer> digits =
    new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));

/**
 * Returns a PIN string that contains four distinct digits.
 */
public static String nextPin() {
    Collections.shuffle(digits);
    final StringBuilder sb = new StringBuilder(4);
    for (Integer digit : digits.subList(0, 4)) {
        sb.append(digit);
    }
    return sb.toString();
}

Obviously, if you wanted the digits as an array of numbers instead of a string, you'd process the sublist differently than I've shown here.显然,如果您希望将数字作为数字数组而不是字符串,则可以以与我在此处显示的不同的方式处理子列表。

If you just return the sublist itself, be aware that it will change each time you如果你只是返回子列表本身,请注意它每次你都会改变

//create list ArrayList numbers = new ArrayList(); //创建列表ArrayList numbers = new ArrayList(); Random randomGenerator=new Random();while (numbers.size() < 4) {int random = randomGenerator .nextInt(4);随机 randomGenerator=new Random();while (numbers.size() < 4) {int random = randomGenerator .nextInt(4); if (!numbers.contains(random)) { numbers.add(random);}} if (!numbers.contains(random)) { numbers.add(random);}}

Somewhat of an academic exercise - here's one requiring Java 8:有点学术性的练习 - 这是一个需要 Java 8 的练习:

    // flag to control if you want number sequence to be the same each run
    boolean repeatable = true;

    // seed for randomness - for permutation of list (not the integers)
    Random rnd = new Random((repeatable ? 3 : System.currentTimeMillis()));

    // generate randomized sequence as a List
    List<Integer> myNums;
    Collections.shuffle((myNums = IntStream.rangeClosed(1000, 9999).boxed().collect(Collectors.toList())), rnd);

    // Work with list...
    for (Integer somePin : myNums) {
        Log.i("", "Next PIN: "+somePin);
    }

You're gonna have to add the random ints step after step and check for duplicates.您将不得不一步一步地添加随机整数并检查重复项。

Random random = new Random();
int rdmInt = random.nextInt(9);
String pin = "";
while (pin.length() < 4) {
    rdmInt = random.nextInt(9);
    String addition = String.valueOf(rdmInt);
    if (pin.contains(addition)) continue;
    pin += addition;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM