【知识积累】随机数生成的几种方法

一、前言

  在我们平时写代码的时候,免不了会使用到随机数,特此将几种随机的生成总结如下。

二、随机数生成

  对于随机数的生成,分为四种情况,假设两个数为min, max,则有如下四种情况。

  1. (min, max),表示生成的随机数不包括min和max。

  2. [min, max),表示生成的随机数包括min,但不包括max。

  3. (min, max],表示生成的随机数不包括min,但是包括max。

  4. [min, max],表示生成的随机数包min,也包括max。

  下面我们就上面的四种情况使用三种不同的方法实现。

  2.1 使用Math.random方法

  其代码如下  

复制代码
package com.hust.grid.leesf.random;

/**
 * 使用Math.random方法生成随机数
 * @author LEESF
 * 2016.3.30
 */
public class RandomTest {
    //(min, max)
    public static int random1(int min, int max) {
        int ran;
        while ((ran = (int) (Math.random() * (max - min) + min)) == min);
        return ran;
    }
    
    //[min, max)
    public static int random2(int min, int max) {
        int ran = (int) (Math.random() * (max - min) + min);
        return ran;
    }
    
    // (min, max]
    public static int random3(int min, int max) {
        int ran;
        while ((ran = (int) (Math.random() * (max - min + 1) + min)) == min);
        return ran;
    }
    
    //[min, max] 
    public static int random4(int min, int max) {
        int ran = (int) (Math.random() * (max - min + 1) + min);
        return ran;
    }
    
    public static void main(String[] args) {
        int min = 40;
        int max = 100;
        // (min, max)
        System.out.println(random1(min, max));
        // [min, max)
        System.out.println(random2(min, max));
        // (min, max]
        System.out.println(random3(min, max));
        // [min, max]
        System.out.println(random4(min, max));
    }
}
View Code
复制代码

  运行结果 

复制代码
59
49
57
45
View Code
复制代码

  2.2 使用Random对象的nextInt方法

  其代码如下 

复制代码
package com.hust.grid.leesf.random;

import java.util.Random;

/**
 * 使用Random对象生成随机数
 * 
 * @author LEESF 2016.3.30
 */
public class RandomTest {
    // (min, max)
    public static int random1(int min, int max) {
        Random random = new Random();
        int seed = max - min;
        int ran;
        while ((ran = random.nextInt(seed) + min) == min)
            ;
        return ran;
    }

    // [min, max)
    public static int random2(int min, int max) {
        Random random = new Random();
        int seed = max - min;
        int ran = random.nextInt(seed) + min;
        return ran;
    }

    // (min, max]
    public static int random3(int min, int max) {
        Random random = new Random();
        int seed = max - min + 1;
        int ran;
        while ((ran = (int) (random.nextInt(seed) + min)) == min)
            ;
        return ran;
    }

    // [min, max]
    public static int random4(int min, int max) {
        Random random = new Random();
        int seed = max - min + 1;
        int ran = random.nextInt(seed) + min;
        return ran;
    }

    public static void main(String[] args) {
        int min = 40;
        int max = 100;
        // (min, max)
        System.out.println(random1(min, max));
        // [min, max)
        System.out.println(random2(min, max));
        // (min, max]
        System.out.println(random3(min, max));
        // [min, max]
        System.out.println(random4(min, max));
    }
}
View Code
复制代码

  运行结果  

复制代码
76
63
66
93
View Code
复制代码

  2.3 使用System类的currentTimeMillis方法

  这种方式的随机数不是随机的,但是在不严格的情况可以使用,可以用作参考,代码如下

复制代码
package com.hust.grid.leesf.random;


/**
 * 使用System类生成随机数
 * 
 * @author LEESF 2016.3.30
 */
public class RandomTest {
    // (min, max)
    public static int random1(int min, int max) {
        int random;
        while ((random = (int) (System.currentTimeMillis() % (max - min) + min)) == min)
            ;
        return random;
    }

    // [min, max)
    public static int random2(int min, int max) {
        long currentTime = System.currentTimeMillis();
        int random = (int) (currentTime % (max - min));
        return random;
    }

    // (min, max]
    public static int random3(int min, int max) {
        int random;
        while ((random = (int) (System.currentTimeMillis() % (max - min + 1) + min)) == min)
            ;
        return random;
    }

    // [min, max]
    public static int random4(int min, int max) {
        int random = (int) (System.currentTimeMillis() % (max - min + 1) + min);
        return random;
    }

    public static void main(String[] args) {
        int min = 40;
        int max = 100;
        // (min, max)
        System.out.println(random1(min, max));
        // [min, max)
        System.out.println(random2(min, max));
        // (min, max]
        System.out.println(random3(min, max));
        // [min, max]
        System.out.println(random4(min, max));
    }
}
View Code
复制代码

  运行结果

复制代码
65
25
62
62
View Code
复制代码

三、总结

  对随机数生成的几种方法进行了总结,在以后需要的时候直接可以使用,平时多进行积累。谢谢各位园友的观看~

posted @   leesf  阅读(1334)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示