zoukankan      html  css  js  c++  java
  • 学习Java的第十六天——随机数

    学习内容:随机数

    1.GetEvenNum()方法

    实例代码:

    package 数字处理类;

    public class MathRondom {
    public static int GetEvenNum(double num1,double num2) {
    //产生num1~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) {
    // TODO 自动生成的方法存根
    System.out.println("任意一个2~32之间的偶数:"+GetEvenNum(2,32));
    }

    }

    运算结果

    产生一个随机整数:1913853499
    产生一个0~10之间的整数:5
    产生一个布尔型的值:true
    产生一个双精度型的值:0.9298156911466924

    2.GetRandomChar()方法

    实例代码:

    package 数字处理类;

    public class MathRandomChar {
    public static char GetRandomChar(char c1,char c2) {
    return (char)(c1+Math.random()*(c2-c1+1));
    }
    public static void main(String[] args) {
    // TODO 自动生成的方法存根
    System.out.println("任意小写字母:"+GetRandomChar('a','z'));
    System.out.println("任意大写字母:"+GetRandomChar('A','Z'));
    System.out.println("0~9任意数字字符:"+GetRandomChar('0','9'));
    }

    }

    运算结果:

    任意小写字母:w
    任意大写字母:P
    0~9任意数字字符:0

    3.Random类

    实例代码:

    package 数字处理类;

    import java.util.Random;

    public class RandomDemo {

    public static void main(String[] args) {
    // TODO 自动生成的方法存根
    Random r=new Random();
    System.out.println("产生一个随机整数:"+r.nextInt());
    System.out.println("产生一个0~10之间的整数:"+r.nextInt(10));
    System.out.println("产生一个布尔型的值:"+r.nextBoolean());
    System.out.println("产生一个双精度型的值:"+r.nextDouble());
    }

    }

    运算结果:

    产生一个随机整数:-1764077992
    产生一个0~10之间的整数:4
    产生一个布尔型的值:true
    产生一个双精度型的值:0.7065045807910766

    明天任务:大数字运算

  • 相关阅读:
    The xp_cmdshell proxy account information cannot be retrieved or is invalid. Verify that the '##xp_cmdshell_proxy_account##' credential exists and contains valid information.
    SQL SERVER-创建Alwayson
    Powershell-远程操作
    powershell-将powershell脚本排到JOB
    powershell-脚本运行权限政策
    Winserver-默认以管理员运行程序
    Powershell-创建Module
    SQL SERVER-查询爆破sa密码的主机
    power-virus
    141.Linked List Cycle 快慢指针
  • 原文地址:https://www.cnblogs.com/zyj3955/p/13356379.html
Copyright © 2011-2022 走看看