zoukankan      html  css  js  c++  java
  • JAVA Random 随机类

    nextInt 方法 得到一个随机整数, 可以指定范围

    package object;
    import static net.util.Print.*;
    import java.util.Random;
    
    public class Test{
        public static void main(String[] args){
            Random rand = new Random();
            for(int i=0;i<100;i++){
            int j = rand.nextInt(100)+1;//调用Rand 方法 
            printnb(j+"	");
            }
            
        }
    
    }
    /*******************here is Rand nextInt display **************************

    public int nextInt(int bound) {
    if (bound <= 0)
    throw new IllegalArgumentException(BadBound);

    
    

    int r = next(31);
    int m = bound - 1;
    if ((bound & m) == 0) // i.e., bound is a power of 2
    r = (int)((bound * (long)r) >> 31);
    else {
    for (int u = r;
    u - (r = u % bound) + m < 0;
    u = next(31))
    ;
    }
    return r;
    }*///~

     

    nextFloat 方法 得到一个随机浮点数,不能指定范围

    package object;
    import static net.util.Print.*;
    import java.util.Random;
    
    public class Test{
        public static void main(String[] args){
            Random rand = new Random();
            for(int i=0;i<100;i++){
            float j = rand.nextFloat();
            printnb(j+"	");
            }
            
        }
    
    }
    /*****************this is nextFloat show***********************
     * public float nextFloat() {
     *        return next(24) / ((float)(1 << 24));
     *    }
    *///~
  • 相关阅读:
    python-Lock进程同步解决互斥
    python-Event事件处理进程同步
    python-queue队列通信
    python-无名管道进程通信
    python-signal
    python-购物车
    python-多进程类封装
    python-哈夫曼树
    python-双向链表
    openstack 开发step-by-step
  • 原文地址:https://www.cnblogs.com/jiangfeilong/p/9932760.html
Copyright © 2011-2022 走看看