0

I was working on a project where we needed to generate a unique number for the Firebase Realtime Database. Now I want to generate a random 8- to 12-digit number that will be unique. Can anyone provide a suitable method/algorithm for obtaining the number, or can it be combined with string?

Aashis
  • 41
  • 1
  • 8
  • Did you try UIID? – Md. Enamul Haque Apr 19 '22 at 08:39
  • `String id = UUID.randomUUID().toString();` did you try UUID as @Md.EnamulHaque mentioned? – Usama Altaf Apr 19 '22 at 08:45
  • i try val id: String = UUID.randomUUID().toString(), It generate more digit than 12 eg, #b3907a05-bc1b-4f55-abbc-3d94b2ea33c7 – Aashis Apr 19 '22 at 10:11
  • Do `val random = abs(Random(LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)).nextLong())` and that will give you a very long number. Then check if it's at least `length` long (whatever you need) if it's not, pad with zeros or similar, then extract the last `length` digits that you need (start from the right). Ultimately, a 12-digit number is "not very unique" depending on your needs (which you haven't provided) if it's for unique identifier, I'd use a UUID string, as the chances of collisions are *very* small. – Martin Marconcini Apr 19 '22 at 10:25
  • 111.111.111.111 is not "a big number" depending on your needs. – Martin Marconcini Apr 19 '22 at 10:27

3 Answers3

0

If you truly need a random number with NN digits you should use Kotlin's Random:

 val random = abs((0..999999999999).random()) 

Of course, in this, 0 is a valid number in this sequence. So what do you do in in the 111 billion chances you get 0? well, that's up to you. You could change the range (but less numbers = less randomness)

You could "pad" 0s to your number so 123 becomes 000000000123 (as a String, that is). Ultimately what you do with the random number is up 2 you.

Keep in mind Random also takes a Seed.

So you could become more fancy by using the time at UTC as the seed (which is constantly changing every instant):

val random = abs(Random(LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)).nextLong())

This will likely give you HUGE numbers so you should convert to string and take the last NN digits

If you get for eg.:

2272054910131780911

You can take: 910131780911

I have created a simple playground where you can see this in action. Please understand I made this in 10 minutes, so there may be optimizations all over the place, I don't have the Kotlin standard library in my head and the Playground's autocomplete is not the same as Android Studio.

Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144
0

Possibly look into SecureRandom -- Cryptographically strong random number generator (RNG): https://docs.oracle.com/javase/8/docs/api/java/security/SecureRandom.html

Also, see this post regarding how long of a random number to generate by using SecureRandom: How to generate a SecureRandom string of length n in Java?

emonz
  • 4,314
  • 1
  • 10
  • 4
-1

You can use Current Milli Second as Unique Number as follows

/**
 * @param digit is to define how many Unique digit you wnat
 * Such as 8, 10, 12 etc
 * But digit must be Min 8 Max 12
 */
fun getUID(digit:Int):Long{
    var currentMilliSeconds:String = ""+Calendar.getInstance().timeInMillis
    var genDigit:Int = digit
    if(genDigit<8)
        genDigit = 8

    if(genDigit>12)
        genDigit = 12

    var cut = currentMilliSeconds.length - genDigit
    currentMilliSeconds = currentMilliSeconds.substring(cut);
    return currentMilliSeconds.toLong()
}

Call it like

var UID = getUID(12)//parameter value can be 8-12 
Md. Enamul Haque
  • 926
  • 8
  • 14
  • `Calendar.getInstance().timeInMillis` Don't. If anything, use `LocalDateTime.now()` and take what you need from there. This has problems, if you trim some of the timeInMillis, you risk getting the same number(s). – Martin Marconcini Apr 19 '22 at 10:03
  • I think it have high risk of getting the same number as @MartinMarconcini Describe – Aashis Apr 19 '22 at 10:12
  • No @Aashis you can generate till 999999999999 if you generate 12 digit. If current generated number is 650432726017 you can generate 999999999999-650432726017 = 349567273982 new random number without any duplicate. – Md. Enamul Haque Apr 20 '22 at 05:40
  • One issue is that if this is called quickly two or more times it gets same result. I had wery bad bug just because of that and killed some days.. This is no Uniq its time based and can return same string more times on pretty quick cpu.. – Renetik Jul 16 '22 at 01:10
  • I was actually using nanoTime() – Renetik Jul 16 '22 at 01:10