【问题标题】:Generate 6 digit random number [duplicate]生成6位随机数[重复]
【发布时间】:2018-12-21 16:40:33
【问题描述】:

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

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

【问题讨论】:

  • 没有000000这样的号码。编号为0,可以在其显示表示中填充为"000000",但0000000 是相同的编号。如果您需要 6 位数字,则需要从 100000999999 范围内的数字。如果您需要 6 位 字符串,请像您一样生成一个数字(请参阅下面我对 Karol 的评论),然后通过填充到 6 位转换为字符串。
  • 所以你只想生成 100000 到 999999 之间的数字?
  • 数字显示时是否必须显示“000000”而不是“0”?
  • 谢谢,我知道了。
  • RandomStringUtils.randomNumeric(6);

标签: java


【解决方案1】:

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

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

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

这里是代码。

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);
}

【讨论】:

  • 这里还有一件事,你不能得到“000000”作为数字,它只会是“0”,这就是我在这里返回字符串的原因。
  • 我知道了,很好。
  • 此代码无效。有时我会得到超过 6 位数字
  • rnd.nextInt(999999);这将生成一个低于“999999”的随机数,该随机数始终低于 6 位数字,然后将由 String.format("%06d", number);成固定的6位数。所以这不可能是你所说的。
【解决方案2】:

如果您需要一个六位数字,它必须以 100000 开头

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

前导零无效,0000000 相同。如果您使用的是 Java 7+,则可以使用 ThreadLocalRandom 进一步简化它:

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

【讨论】:

  • 899999 -> 900000,因为nextInt 保证生成的数字小于其参数。
  • 第二个语句应该是 ThreadLocalRandom.current().nextInt(100000, 1000000) 以涵盖最多 6 位数字,最多 999999。
【解决方案3】:

我知道这很困难,但你可以这样做: 为 BinaryNumber 创建一个类; 创建一个生成 6 个字符的 char[] 的构造函数,其中每个字符都是使用从 0 到 1 的随机数生成的 覆盖 toSrig() 方法,这样如果你想显示它,它将把数字 char[] 作为字符串返回。然后创建一个 toInt() 方法,该方法使用 for 将字符串 char 逐一转换,并通过将当前数字乘以 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;

【讨论】:

    【解决方案4】:

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

    已经解决了here

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

    【讨论】:

    • public int gen() { Random r = new Random( System.currentTimeMillis() ); return ((1 + r.nextInt(2)) * 10000 + r.nextInt(10000)); }
    • 你可以edit你的答案在上面的评论中包含你的代码块等附加信息。
    【解决方案5】:

    这是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);
    
                            }
         }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 2019-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多