Java--- Random类 和 Math.random()方法

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


Random类

Java.util.Random类,可以通过实例化一个Random对象创建一个随机数生成器。

语法:

Random ran=new Random();
Random ran=new Random(seedValue);

Random类中还提供各种类型随机数的方法:
nextInt():返回一个随机整数(int)
nextInt(int n):返回大于等于0、小于n的随机整数(int)
nextLong():返回一个随机长整型值(long)
nextBoolean():返回一个随机布尔型值(boolean)
nextFloat():返回一个随机浮点型值(float)
nextDouble():返回一个随机双精度型值(double)
nextGaussian():概率密度为高斯分布的双精度值(double)

案例:

import java.util.Random;
 
public class randomTest {
    public static void main(String args[])
    {
        Random random=new Random();//以系统当前时间作为随机数生成的种子
        System.out.println(random.nextInt(10));//返回一个大于0且小于10的整数
        System.out.println(random.nextFloat()); //返回一个随机浮点型
        System.out.println(random.nextBoolean());  //返回一个随机布尔型值
        System.out.println(random.nextDouble());  //返回一个随机双精度型
        System.out.println(random.nextLong());  //返回一个随机长整形
 
    }
}

在这里插入图片描述

Math.random()方法

介绍:

Math.random()是令系统随机选取大于等于 0.0 且小于 1.0的伪随机 double

公式总结:

产生一个[0,1)之间的随机数。
Math.random():
返回大于等于0小于n之间的随机数
int num=(int)(Math.random()*n);
返回指定范围的随机数(m-n之间)的公式:
Math.random()*(n-m)+m; //包括m ,不包括n

或者

Math.random()*(n+1-m)+m //包括m ,也包括n

案例 1:

需求: 返回0-5之前的随机数
(int) (Math.random() * 6)
public class Test2 {
    public static void main(String[] args) {
        int testTimes = 10000000;


        int K = 6;
        int[] counts = new int[6];

        for (int i = 0; i < testTimes; i++) {
            int ans = (int) (Math.random() * K); // [0,K-1]
            counts[ans]++;
        }
        for (int i = 0; i < K; i++) {
            System.out.println(i + "这个数,出现了 " + counts[i] + " 次");
        }
    }
    

}

在这里插入图片描述

案例2: 我们需要取2~22之间的偶数

(int)2+(int)(Math.random()*(22-2));
public class Test03 {

    public static int GetevenNum(double num1,double num2){
        int s=(int)num1+(int)(Math.random()*(num2-num1));
        if(s%2==0){
            return s;
        } else{
            return s+1;
        }

    }
    public static void main(String[] args){
        System.out.println("任意一个num1_num2之间的偶数:"+GetevenNum(2,22));
    }
}

案例 3:

需求:

任意的x,x属于[0,1),[0,x)范围上的数出现概率由原来的x调整成x平方

Math.max(Math.random(), Math.random());

在这里插入图片描述

随机生成字符

例如:

1.随机生成a~z之间的字符

(char)(‘a’+Math.random()*(‘z’-‘a’+1));

2.随机生成cha1~cha2的字符

(char)(cha1+Math.random()*(cha2-cha1+1));

public static void main(String[] args){
        char ch1 = (char) ('a' + Math.random() * ('z' - 'a' + 1));
        System.out.println(ch1);

    }
随机生成a到d 之间的数 . ---------a和d都包括
public static void main(String[] args){

        System.out.println(Test03.getRandomChar('a','d'));//a和d都包括
    }

    public static char getRandomChar(char ch1,char ch2) {
        char ch=(char) (ch1+ Math.random()*(ch2-ch1+1));
        return ch;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值