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

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

一、前言


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


二、随机数生成


  对于随机数的生成,分为四种情况,假设两个数为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));
    }
}

 运行结果 

59
49
57
45

 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));
    }
}

 运行结果 

76
63
66
93

  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));
    }
}

运行结果

65
25
62
62

三、总结


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

目录
相关文章
|
Python
解决安装ConcurrentLogHandler报错error in ConcurrentLogHandler setup command: use_2to3 is invalid.
本文介绍了在Python环境下安装ConcurrentLogHandler时遇到的"use_2to3 is invalid"错误的解决方法,主要是通过降级setuptools到57.5.0版本来解决该问题。
662 2
|
2月前
|
存储 人工智能 搜索推荐
拔俗AI助教系统:基于大模型与智能体架构的新一代教育技术引擎
AI助教融合大语言模型、教育知识图谱、多模态感知与智能体技术,重构“教、学、评、辅”全链路。通过微调LLM、精准诊断错因、多模态交互与自主任务规划,实现个性化教学。轻量化部署与隐私保护设计保障落地安全,未来将向情感感知与教育深度协同演进。(238字)
阿里云免费商标注册查询系统入口链接(支持图片检索)
阿里云商标注册查询系统入口链接,免费商标近似查询工具,支持图形检索功能,上传图片即可搜索查询
7186 0
 阿里云免费商标注册查询系统入口链接(支持图片检索)
|
数据采集 前端开发 算法
Python Requests 的高级使用技巧:应对复杂 HTTP 请求场景
本文介绍了如何使用 Python 的 `requests` 库应对复杂的 HTTP 请求场景,包括 Spider Trap(蜘蛛陷阱)、SESSION 访问限制和请求频率限制。通过代理、CSS 类链接数控制、多账号切换和限流算法等技术手段,提高爬虫的稳定性和效率,增强在反爬虫环境中的生存能力。文中提供了详细的代码示例,帮助读者掌握这些高级用法。
745 1
Python Requests 的高级使用技巧:应对复杂 HTTP 请求场景
|
机器学习/深度学习 传感器 算法
NGO-CNN-SVM分类预测 | Matlab 北方苍鹰算法优化卷积神经网络-支持向量机分类预测
NGO-CNN-SVM分类预测 | Matlab 北方苍鹰算法优化卷积神经网络-支持向量机分类预测
|
存储 前端开发 JavaScript
VSCode调试揭秘:Live Server助力完美测试Cookie与Session,远超“Open in Browser“!
VSCode调试揭秘:Live Server助力完美测试Cookie与Session,远超“Open in Browser“!
|
Oracle 关系型数据库 MySQL
入职必会-开发环境搭建17-IDEA连接数据库
IntelliJ IDEA集成了众多插件,方便开发者使用,使用IDEA自带的Database模块就可以很方便的配置、连接数据库,在 IntelliJ IDEA 中连接数据库,可以按照以下步骤进行操作。
766 0
|
消息中间件 存储 网络协议
即时通讯需要用到哪些技术手段?
该文探讨了即时通讯技术,涉及网络协议(TCP/IP、UDP、HTTP/HTTPS)在IM中的应用,数据传输与同步(消息队列、长轮询、WebSocket、数据同步)技术,安全性保障(加密、认证授权、防止攻击)措施,以及多媒体处理(音频、视频处理和实时传输)和用户界面交互设计的重要性。文章旨在帮助读者理解并应用相关技术。
1187 1
|
Python
python实现学生信息管理系统(附源码 可供大作业或练习使用)
python实现学生信息管理系统(附源码 可供大作业或练习使用)
945 2
python实现学生信息管理系统(附源码 可供大作业或练习使用)
|
存储 安全 JavaScript
SpringBoot中如何使用Cookies
本文介绍了如何在Spring Boot中操作HTTP Cookie。内容包括:使用@CookieValue注解读取Cookie,通过HttpServletResponse设置Cookie,读取所有Cookie,设置Cookie过期时间,理解HTTPS与Cookie安全,使用HttpOnly Cookie防止XSS攻击,以及如何删除Cookie。示例代码展示了各种操作的方法。
402 0