Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

J2ME: How to generate random number?

I just wanted to know how do I generate random number using J2ME CLDC 1.0 MIDP 2.0 ?

Basically I want to generate a 14 digits random number each time when the menu item Generate is clicked from the mobile's screen.

like image 584
Sarfraz Avatar asked Dec 04 '25 10:12

Sarfraz


2 Answers

I'm not really familiar with J2ME, however the Javadoc shows that the Random class is part of the CLDC api, so you can generate a 14 digit number like this:

public static void main(String[] args) {
    Random r = new Random();
    long l = r.nextLong();
    System.out.println(String.format("%015d", l).substring(1, 15));
}
like image 74
Jared Russell Avatar answered Dec 06 '25 22:12

Jared Russell


Random r = new Random();
r.nextInt(bottomX-topX)+topX; //will give you the next random integer in range [bottomX,topX]
like image 32
Vitali Pom Avatar answered Dec 07 '25 00:12

Vitali Pom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!