Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a random 16 digits number, with specific first digits?

Tags:

java

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.

like image 589
Marios Ath Avatar asked Jan 08 '15 15:01

Marios Ath


People also ask

How do you generate a random number from 1 to n?

Approach: Create an array of N elements and initialize the elements as 1, 2, 3, 4, …, N then shuffle the array elements using Fisher-Yates shuffle Algorithm. Fisher–Yates shuffle Algorithm works in O(n) time complexity. The assumption here is, we are given a function rand() that generates a random number in O(1) time.

How do you get the program to generate a random number between 1 and 10?

Java Random number between 1 and 10 Below is the code showing how to generate a random number between 1 and 10 inclusive. Random random = new Random(); int rand = 0; while (true){ rand = random. nextInt(11); if(rand != 0) break; } System.


3 Answers

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.

like image 149
Nic Avatar answered Oct 17 '22 14:10

Nic


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));
like image 25
weston Avatar answered Oct 17 '22 12:10

weston


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);
    }
}
like image 3
sol4me Avatar answered Oct 17 '22 12:10

sol4me