zoukankan      html  css  js  c++  java
  • 类Random

    /*
    * Random:产生随机数的类
    *
    * 构造方法
    * public Random();没有给种子,用的是默认种子,是当前时间的毫秒值
    * public Random(long seed);使用给定的种子,每次生成的随机数一样
    *
    * 成员方法
    * public int nextInt();返回的是int范围内的随机数
    * public int nextInt(int n);返回的是(0,n)范围内的随机数
    * */

    public Random();没有给种子,用的是默认种子,是当前时间的毫秒值

    import java.util.Random;
    import java.util.Scanner;
    
    /*
     * Random:产生随机数的类
     * 
     * 构造方法
     * public Random();没有给种子,用的是默认种子,是当前时间的毫秒值
     * public Random(long seed);使用给定的种子
     * 
     * 成员方法
     * public int nextInt();返回的是int范围内的随机数
     * public int nextInt(int n);返回的是(0,n)范围内的随机数
     * */
    
    public class IntegerDemo {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		Random r = new Random();// 没有给种子,用的是默认种子,是当前时间的毫秒值
    
    		for (int i = 0; i < 10; i++) {
    			// int num=r.nextInt();//返回的是int范围内的随机数
    			int num = r.nextInt(100 + 1);// 返回的是(0,100+1)范围内的随机数
    
    			System.out.println(num);
    		}
    	}
    }
    

    public int nextInt(int n);返回的是(0,n)范围内的随机数

    import java.util.Random;
    import java.util.Scanner;
    
    /*
     * Random:产生随机数的类
     * 
     * 构造方法
     * public Random();没有给种子,用的是默认种子,是当前时间的毫秒值
     * public Random(long seed);使用给定的种子,每次生成的随机数一样
     * 
     * 成员方法
     * public int nextInt();返回的是int范围内的随机数
     * public int nextInt(int n);返回的是(0,n)范围内的随机数
     * */
    
    public class IntegerDemo {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		Random r = new Random(1000);// 使用给定的种子,每次生成的随机数一样
    
    		for (int i = 0; i < 10; i++) {
    			// int num=r.nextInt();//返回的是int范围内的随机数
    			int num = r.nextInt(100 + 1);// 返回的是(0,100+1)范围内的随机数
    
    			System.out.println(num);
    		}
    	}
    }
    
  • 相关阅读:
    配置PyDev,开始eclipsePython之旅
    PyDev下PyQt 的尝试
    逻辑回归 C++
    HP Unix vsftp服务配置
    线性回归(最小二乘法、批量梯度下降法、随机梯度下降法、局部加权线性回归) C++
    批量梯度下降(Batch gradient descent) C++
    利用expect验证主机口令
    python Paramiko 模块远程管理主机
    文件系统巡检
    awk查找特定字段
  • 原文地址:https://www.cnblogs.com/denggelin/p/6284790.html
Copyright © 2011-2022 走看看