0

I am trying to create a random number use SecureRandom. It must be digits only (0123456789) and be 12 characters long. Any help would be greatly appreciated.

    SecureRandom secureRandom = new SecureRandom();
    synchronized (secureRandom) {
        final byte[] random = new byte[12];
        secureRandom.nextBytes(random);
        return Base64.encodeBase64URLSafeString(random);
    }

I am getting the following:

IHmv_qsWMZ0teI_-
newPHPDev
  • 33
  • 5
  • 1
    Base64 encoding largely uses letters – g00se Jul 21 '23 at 12:26
  • 1
    You will get digits for each element of that array with `(0xFF & random[i]) % 10)` – g00se Jul 21 '23 at 12:35
  • 1
    There's no point synchronizing on `secureRandom` as no other thread can possibly have a reference to it. – tgdavies Jul 21 '23 at 13:02
  • @tgdavies ok thanks. but how would i come up with a solution for a randm 12 digit number. – newPHPDev Jul 21 '23 at 13:05
  • @g00se has already explained that. – tgdavies Jul 21 '23 at 13:06
  • ok, trying to figure out a solution based on that. a bit confused. @g00se on how to accomplish. – newPHPDev Jul 21 '23 at 13:13
  • What are you confused about? – g00se Jul 21 '23 at 13:34
  • You could ask `nextBytes` for just 5 random bytes. This is 5*8=40 bits. So the result can then be converted into a single integer number between 0 and 2^40. And 2^40 is just 10% above 10^12. You can probably afford to use plain rejection for those numbers that have the misfortune to lie above 10^12. – jpmarinier Jul 21 '23 at 13:49
  • The easy solution is simply `num = secureRandom.nextLong() % 1000000000000L;` – President James K. Polk Jul 21 '23 at 14:00
  • Wouldn't using `%10` introduce a modulo bias in the resulting values? As I understand it, this is highly undesirable, especially if used for encryption. – WJS Jul 21 '23 at 15:40
  • It *could* do. I'm not sure of the mathematics of it – g00se Jul 21 '23 at 15:59
  • I certainly wouldn't advance my comment as a solution. It was really just a reaction to *the existing array's presence* – g00se Jul 21 '23 at 16:27
  • Does this answer your question? [How do I generate random integers within a specific range in Java?](https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java) – ryanwebjackson Jul 24 '23 at 00:03

1 Answers1

0

I am trying to create a random number using SecureRandom. It must be digits only (0123456789) and be 12 characters long.

You could doit like this. Just specify the origin and bound to match your digit count requirements.

SecureRandom secureRandom = new SecureRandom();
long random =  secureRandom.nextLong(100_000_000_000L,1_000_000_000_000L);
System.out.println(random);

prints something like

426491911225

If it must be in a byte array of individual digits, you can do the following:

byte[] bytes = new byte[12];
int loc = bytes.length - 1; // insert in reverse in array

while(random > 0) {
    bytes[loc--] = (byte)(random%10);
    random/=10;
}
System.out.println(Arrays.toString(bytes));

prints

[4, 2, 6, 4, 9, 1, 9, 1, 1, 2, 2, 5]
WJS
  • 36,363
  • 4
  • 24
  • 39