zoukankan      html  css  js  c++  java
  • Java编程中Math.random()的应用(随机生成数的应用)

    <1>随即范围
    Java中a=Math.random()的取值范围为
    0<=a<=1

    由此可得
    a=Math.random()*n的取值范围是
    0<=a<=n

    <2>实际应用

    对于要求随机输出一个大写字母

    char ss=(char)(int)(26*(Math.random())+65);//表示随机一个大写字母
    System.out.println(""+ss);

    对已要求随机输出一个小写字母

    char es=(char)(int)(26*(Math.random())+97);//表示随机一个小写字母
    System.out.println(""+es);

    在进行大写字母到小写字母的转换时,通过对数字码进行改变(大写到小写+32,小写到大写-32),再进行类型转换即可

    以下是实际运用
    源代码:

    public class First{
    
        public static void main(String args[]){
        char ss=(char)(int)(26*(Math.random())+65);//表示随机一个大写字母
        System.out.println(""+ss);
        char es=(char)(int)(26*(Math.random())+97);//表示随机一个小写字母
        System.out.println(""+es);
    
        }
    
    }

    运行结果:
    Y
    o

  • 相关阅读:
    状压DP
    string
    hdu3068
    HDU Stealing Harry Potter's Precious(状压BFS)
    状压BFS
    BFS+打印路径
    poj Meteor Shower
    C语言-无符号数与有符号数不为人知的秘密
    keras_实现cnn_手写数字识别
    python_plot画图参数设置
  • 原文地址:https://www.cnblogs.com/Kaniso-Vok/p/13756276.html
Copyright © 2011-2022 走看看