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. I have created a simple playground where you can see this in action. This will likely give you HUGE numbers so you should convert to string and take the last NN digits Val random = abs(Random(LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)).nextLong()) So you could become more fancy by using the time at UTC as the seed (which is constantly changing every instant): Ultimately what you do with the random number is up 2 you. You could "pad" 0s to your number so 123 becomes 000000000123 (as a String, that is). You could change the range (but less numbers = less randomness) So what do you do in in the 111 billion chances you get 0? well, that's up to you. But you can also use Maven if you are more comfortable with it. Of course, in this, 0 is a valid number in this sequence. Gradle is the most commonly used build tool in Kotlin, and it provides a Kotlin DSL which is used by default when generating a Kotlin project, so this is the recommended choice. If you truly need a random number with NN digits you should use Kotlin's Random: val random = abs((0.999999999999).random())
0 Comments
Leave a Reply. |