I want to create a randomly generated 16 digit-number in java.But there is a catch I need the first two digits to be "52". For example, 5289-7894-2435-1967. I was thinking of using a random generator and create a 14 digit number and then add an integer 5200 0000 0000 0000. I tried looking for similar problems and can't really find something useful. I'm not familiar with the math method,maybe it could solve the problem for me.
Asked
Active
Viewed 2.8k times
8
-
4Generate (pseudo)random number. Prefix it with '52'? – matcheek Jan 08 '15 at 15:42
-
One google search. The rest should be easy with this. http://stackoverflow.com/questions/3709521/how-do-i-generate-a-random-n-digit-integer-in-java-using-the-biginteger-class – austin wernli Jan 08 '15 at 15:45
-
Yes,I've made that thought as well, but I don't know how to do it. This was my problem to begin with. – Marios Ath Jan 08 '15 at 15:45
-
You should probably look at these two : http://stackoverflow.com/questions/4655931/12-digit-unique-random-number-generation-in-java http://stackoverflow.com/questions/5328822/generating-10-digits-unique-random-number-in-java – user1676389 Jan 08 '15 at 15:49
-
Your idea, to randomly create a 14-digit number and then add 5200000000000000 to it (or convert it to a string and prepend `"52"`) makes sense. Do you care about whether every possible 14-digit number could be generated, or is it acceptable if your random number generator misses some possibilities? Do you care about how uniform the distribution is? – ajb Jan 08 '15 at 15:50
-
Or Google "generate random mastercard number" – Icemanind Jan 08 '15 at 16:10
4 Answers
8
First, you need to generate a random 14-digit number, like you've done:
long first14 = (long) (Math.random() * 100000000000000L);
Then you add the 52
at the beginning.
long number = 5200000000000000L + first14;
One other way that would work equally well, and will save memory, since Math.random()
creates an internal Random
object:
//Declare this before you need to use it
java.util.Random rng = new java.util.Random(); //Provide a seed if you want the same ones every time
...
//Then, when you need a number:
long first14 = (rng.nextLong() % 100000000000000L) + 5200000000000000L;
//Or, to mimic the Math.random() option
long first14 = (rng.nextDouble() * 100000000000000L) + 5200000000000000L;
Note that nextLong() % n
will not provide a perfectly random distribution, unlike Math.random()
. However, if you're just generating test data and it doesn't have to be cryptographically secure, it works as well. It's up to you which one to use.

Nic
- 6,211
- 10
- 46
- 69
3
You can generate 14 random digits and then append at the beginning "52". E.g.
public class Tes {
public static void main(String[] args) {
System.out.println(generateRandom(52));
}
public static long generateRandom(int prefix) {
Random rand = new Random();
long x = (long)(rand.nextDouble()*100000000000000L);
String s = String.valueOf(prefix) + String.format("%014d", x);
return Long.valueOf(s);
}
}

sol4me
- 15,233
- 5
- 34
- 34
-
You could also add the `52` by adding `5200000000000000` (plus or minus a few zeros) to the `x`, if you don't want to use `String` processes to add numbers. It feels cleaner to me. – Nic Jan 08 '15 at 15:48
-
Actually, the code you've posted, that uses the `String` to add a prefix, is wrong. It will fail if `x` is less than 10^13. You'd need something like `String.format("%014d", x)`. – ajb Jan 08 '15 at 16:15
-
Are the digits of `(long)(rand.nextDouble()*100000000000000L)` uniformly distributed? Would probably be better to use this: http://stackoverflow.com/a/2546186/1178016 – xehpuk Jan 08 '15 at 16:33
3
Random rand = new Random();
String yourValue = String.format((Locale)null, //don't want any thousand separators
"52%02d-%04d-%04d-%04d",
rand.nextInt(100),
rand.nextInt(10000),
rand.nextInt(10000),
rand.nextInt(10000));

weston
- 54,145
- 21
- 145
- 203
0
- Create a 14 random digit number. with
Math.random
- Concatenate to it at the begining the "52"
String
. - Convert the string with
Integer.parseInt(String)
method

Jordi Castilla
- 26,609
- 8
- 70
- 109
-
Unfortunately you haven't explained how to do the first step. This is not trivial, since `Random.nextLong()` doesn't allow you specify a bound, unlike `Random.nextInt(bound)`. – ajb Jan 08 '15 at 15:47