带有长度,限制,字符和字母的Java随机数

时间:2016-10-31 17:00:36

标签: java random

您好我是Java的新手,我正在尝试生成随机数字的11位随机数。如何以“[xxx] -xxx #AxAxx”格式执行此操作,其中x是数字0-9,A是任何大写字母。括号,短划线和散列也必须处于正确的位置。另外限制是最后两位数不能是5或6而第一位数不能是0.最好的方法是什么?你必须使用字符串和随机类吗?感谢。

2 个答案:

答案 0 :(得分:1)

FWIW,你可以做到这一点,没有循环的坏值拒绝或黑客添加前导零:

import static java.lang.String.format;

import java.util.Random;

class Generator {
  Random random = new Random();

  private int not5or6() {
    int val = random.nextInt(8);
    return val < 5 ? val : val + 2;
  }

  String randomKey() {
    StringBuilder s = new StringBuilder();
    s.append('[');
    s.append(random.nextInt(900) + 100);
    s.append("]-");
    s.append(format("%03d", random.nextInt(1000)));
    s.append('#');
    s.append((char) ('A' + random.nextInt(26)));
    s.append(random.nextInt(10));
    s.append((char) ('A' + random.nextInt(26)));
    s.append(not5or6());
    s.append(not5or6());
    return s.toString();
  }

  // Or if you you don't like StringBuilder, here's another way...
  String randomKey2() {
    return format("[%d]-%03d#%c%d%c%d%d", 
        random.nextInt(900) + 100, 
        random.nextInt(1000), 
        (char) ('A' + random.nextInt(26)),
        random.nextInt(10), 
        (char) ('A' + random.nextInt(26)),
        not5or6(),
        not5or6());
  }

  public static void main(String[] args) {
    Generator g = new Generator();
    for (int i = 0; i < 100; i++) System.out.println(g.randomKey());
  }
}

答案 1 :(得分:0)

不确定是否有&#34; easy&#34;这样做的方法。

你可以打电话给&#34; nextInt()&#34;在你想要生成的每个部分的随机数生成器上,然后将所有部分放在一起,例如......

import java.util.Random;

public class Rnd {

    private static Random rnd = new Random();

    public static void main(String[] args) {
        for(int k = 0; k < 1000; k++) {
            System.out.println(Rnd.generate());
        }
    }

    private static String generate() {
        // Generate all of the random parts of the desired pattern...

        // First part, 000-999
        String num1 = generateNumbers(1000);
        // Second part, 000-999
        String num2 = generateNumbers(1000);
        // Third part A...Z
        char char1 = generateChar();
        // Forth part, 0-9
        String num3 = generateNumbers(10);
        // Fifth part A...Z
        char char2 = generateChar();
        // Sixth part, 00-99
        String num4 = "56";

        // Make sure last two numbers are not a 5 or 6
        while(num4.contains("5") || num4.contains("6")) {
            num4 = generateNumbers(100);
        }

        return "[" + num1 + "]-" + num2 + "#" + char1 + num3 + char2 + num4;
    }

    private static char generateChar() {
        // Generate a number between 0 and 25 inclusive then add 'A' to it
        return (char) (rnd.nextInt(26) + 'A');
    }

    private static String generateNumbers(int i) {
        // Generates a random int between 0 (inclusive) and i (exclusive)
        // (where i should be a power of 10 that is > 1)
        // Add i to the generated random number, turn it into a string and then strip first character
        // This will ensure a number like 3 will come out as 003 for i = 1000
        return ("" + (rnd.nextInt(i) + i)).substring(1);
    }
}

这会输出类似......

[745]-770#M6U88
[481]-779#N0N82
[182]-777#S2P08
[401]-219#H6O78
[032]-181#O8E82
[579]-949#I0S02
[025]-810#K2P39
[523]-663#L0I89
[560]-084#N5W01
[915]-767#F2A97
[059]-324#R0D79
etc.