J2ME:如何生成随机数?

发布于 2024-08-17 16:56:25 字数 94 浏览 2 评论 0原文

我只是想知道如何使用 J2ME CLDC 1.0 MIDP 2.0 生成随机数?

基本上我想每次菜单项生成一个 14 位随机数 从移动设备屏幕上单击“生成”。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泛泛之交 2024-08-24 16:56:25

我不太熟悉 J2ME,但是 Javadoc 显示Random 类是 CLDC api 的一部分,因此您可以生成一个 14 位数字,如下所示:

public static void main(String[] args) {
    Random r = new Random();
    long l = r.nextLong();
    System.out.println(String.format("%015d", l).substring(1, 15));
}

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));
}
埖埖迣鎅 2024-08-24 16:56:25
Random r = new Random();
r.nextInt(bottomX-topX)+topX; //will give you the next random integer in range [bottomX,topX]
Random r = new Random();
r.nextInt(bottomX-topX)+topX; //will give you the next random integer in range [bottomX,topX]
情绪失控 2024-08-24 16:56:25

您可以使用 MIDP 的 Random 类 中的那个

,或者 CLDC 1.1 可以做 nextLong然后截断,或使用 next(44) 并从那里迭代以获得真正的 14 位数长度。

You can use the Random class of MIDP, or the one in CLDC 1.1

You could do nextLong and then truncate, or use next(44) and iterate from there to have a real 14-number long.

像极了他 2024-08-24 16:56:25
import java.util.Random;

private static void showRandomInteger(int aStart, int aEnd){
        Random generator = new Random();
        generator.setSeed(System.currentTimeMillis());
        if ( aStart > aEnd ) {
          throw new IllegalArgumentException("Start cannot exceed End.");
        }
        //get the range, casting to long to avoid overflow problems
        long range = (long)aEnd - (long)aStart + 1;
        // compute a fraction of the range, 0 <= frac < range
        long fraction = (long)(range * generator.nextDouble());
        int randomNumber =  (int)(fraction + aStart);
        System.out.println("Generated : " + randomNumber);
      }

您可以使用这种通用方法来计算任何范围内的随机数。

import java.util.Random;

private static void showRandomInteger(int aStart, int aEnd){
        Random generator = new Random();
        generator.setSeed(System.currentTimeMillis());
        if ( aStart > aEnd ) {
          throw new IllegalArgumentException("Start cannot exceed End.");
        }
        //get the range, casting to long to avoid overflow problems
        long range = (long)aEnd - (long)aStart + 1;
        // compute a fraction of the range, 0 <= frac < range
        long fraction = (long)(range * generator.nextDouble());
        int randomNumber =  (int)(fraction + aStart);
        System.out.println("Generated : " + randomNumber);
      }

you can use this general method for calculating random numbers in any range.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文