首页 > 其他分享 >Random类

Random类

时间:2022-09-23 16:47:11浏览次数:32  
标签:java String int Random static public

Random()

Random类的作用是生成随机数

import java.util.Random;

public class RandomDemo1 {
    public static void main(String[] args) {
        Random r =new Random();//在没带参数构造函数生成的Random对象的种子缺省是当前系统时间的毫秒数
        for (int i=0;i<10;i++){
            System.out.print(r.nextInt(10)+"\t");//[0,10)
        }
    }
}

Random(long seed)

import java.util.Random;

public class RandomDemo2 {
    public static void main(String[] args) {
        Random r=new Random(7);
        /*Random()里面的数叫做种子,种子数只是随机算法的起源数字,和生成的随机数的区间没有任何关系。*/
        for (int i=0;i<10;i++){
            System.out.print(r.nextInt(10)+"\t");//不论执行几次都是同一个序列
        }
    }
}

无论几次都是这个结果

标签:java,String,int,Random,static,public
From: https://www.cnblogs.com/ben10044/p/16723259.html

相关文章

  • C++ populate template array via random generator and finally sort,print
    #pragmaonce#pragmacomment(lib,"rpcrt4.lib")#include<algorithm>#include<cstring>#include<iostream>#include<random>#include<vector>#include<Windo......
  • vc++ get random via random_device,mt19937
     #include<ctime>#include<iostream>#include<random>usingnamespacestd;staticrandom_devicerd;staticmt19937mt{rd()};template<typenameT>vo......
  • vc++template function get random and quick sort
    //ConsoleApplication2.cpp:Thisfilecontainsthe'main'function.Programexecutionbeginsandendsthere.//#include<ctime>#include<iostream>#include......
  • mt19937_64 get random numbers
    #include<ctime>#include<iostream>#include<random>usingnamespacestd;voiduInt32Array(intlen);intmain(intargs,char**argv){uInt32Array(a......
  • 关于 Math.random()生成指定范围内的随机数的公式推导
    关于Math.random()生成指定范围内的随机数的公式推导在java中,用于生成随机数的Math方法random()只能生成0-1之间的随机数,而对于生成指定区间,例如a-b之间的随机......
  • randomize( ) with Arguments
    当调用不带参数的randomize()方法时,它会为对象中的所有随机变量(声明为rand或randc)分配新值,满足所有约束。当使用参数调用randomize()时,这些参数指定该对象内的随机......
  • Python random 的使用
    导入importrandom 随机数生成//用来生成[a,b]之间的随意整数,包括两个边界值。print(random.randint(0,9))//用来随机生成一个0到1之间的浮点数,包括零。0.15790......
  • ItemRank: A Random-Walk Based Scoring Algorithm for Recommender Engines
    目录概符号说明本文方法GoriM.andPucciA.ItemRank:arandom-walkbasedscoringalgorithmforrecommenderengines.InInternationalJointConferencesonArt......
  • Random类、ArrayList类
    Random类什么是Random类此类的实例用于生成伪随机数。例如,以下代码使用户能够得到一个随机数:Randomr=newRandom();inti=r.nextInt();Random使用步骤......
  • 学习:python 内置模块random
    importrandom#引入模块#创建的文件名项目的名字不要与引入的模块名重复r1=random.randint(1,6)#生成范围随机数r2=random.uniform(1,6)#生成指定范围随机浮点数r3=......