-1

I have a requirement where users will have to see a pin generated for an order and has to be shared/used as authorization code for that particular order.

So I am looking for a library which can generate random unique pins of 6 digit length which can be used for each transaction.

Please share your thoughts on this..

Thanks..

chmk
  • 57
  • 1
  • 8
  • 1
    have you done any research about "How do I generate random integers within a specific range in Java?" – jhamon Jun 11 '19 at 10:01
  • With only 6 digits, the generated pins can only be unique for a short period of time. – Eran Jun 11 '19 at 10:01
  • 1
    you can find your answer here : [link](https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java) and your range would be between 100000 and 999999 – fahime Jun 11 '19 at 10:05
  • `int n = 100000 + random_float() * 900000;` and already a similar answer is [here](https://stackoverflow.com/questions/5392693/java-random-number-with-given-length) – Sudhir Ojha Jun 11 '19 at 10:35
  • Why is this question downvoted? – Umar Farooq Khawaja Nov 07 '19 at 09:19

4 Answers4

3

You can try SecureRandom

Extract From here

java.security.SecureRandom class: This class provides a cryptographically strong random number generator (RNG). A cryptographically strong random number minimally complies with the statistical random number generator tests specified in FIPS 140-2, Security Requirements for Cryptographic Modules, section 4.9.1. Additionally, SecureRandom must produce non-deterministic output. Therefore any seed material passed to a SecureRandom object must be unpredictable, and all SecureRandom output sequences must be cryptographically strong.

java.util.Random class: The classes defined in Random are not cryptographically strong, and the numbers chosen are not completely random because a definite mathematical algorithm (based on Donald E. Knuth’s subtractive random number generator algorithm) is used to select them. Therefore, it is not safe to use this class for tasks that require high level of security, like creating a random password etc.

Example using SecureRandom:

 SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
 int myInt = sr.nextInt(9000000) + 1000000;
TechFree
  • 2,600
  • 1
  • 17
  • 18
1

Please use the Apache library.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.6</version>
</dependency>

int random = RandomUtils.nextInt(1, 7);

Ref: https://www.java67.com/2018/01/3-ways-to-generate-random-integers-on.html

Alexpandiyan Chokkan
  • 1,025
  • 1
  • 10
  • 30
0

I am looking for a library which can generate random unique pins

If you generate a random integer, it's not guaranteed to always be unique. With 6 numeric digits, there's a one in a million chance that users could get the same order number. You could increase the number of digits or also allow alphabet characters to decrease the chances of getting the same order number.

However, the only way to truly ensure each order number is unique is to keep a running tally of total number of orders. Then each user's order number is literally the n'th order you've received.

MadManCam
  • 1
  • 1
-3

You can manually generate pin in java using built-in **java.util.Random class **. You can use this in authentication to generate six digits random number pin.

 int rand_int1 = rand.nextInt(1000000); 
 if rand_int1 has 6 digits
             print(rand_int1)
 else
          generate(again)

this will generate a six digit random number.