当前位置: 移动技术网 > IT编程>开发语言>Java > Java编程中随机数的生成方式总结

Java编程中随机数的生成方式总结

2019年07月22日  | 移动技术网IT编程  | 我要评论

本章先讲解java随机数的几种产生方式,然后通过示例对其进行演示。
广义上讲,java中的随机数的有三种产生方式:
(01). 通过system.currenttimemillis()来获取一个当前时间毫秒数的long型数字。
(02). 通过math.random()返回一个0到1之间的double值。
(03). 通过random类来产生一个随机数,这个是专业的random工具类,功能强大。第1种 利用system.currenttimemillis()获取随机数
通过system.currenttimemillis()来获取随机数。实际上是获取当前时间毫秒数,它是long类型。使用方法如下:

final long l = system.currenttimemillis();

若要获取int类型的整数,只需要将上面的结果转行成int类型即可。比如,获取[0, 100)之间的int整数。方法如下:

final long l = system.currenttimemillis();
final int i = (int)( l % 100 );

第2种 利用math.random()获取随机数
通过math.random()来获取随机数。实际上,它返回的是0(包含)到1(不包含)之间的double值。使用方法如下:

final double d = math.random();

若要获取int类型的整数,只需要将上面的结果转行成int类型即可。比如,获取[0, 100)之间的int整数。方法如下:

final double d = math.random();
final int i = (int)(d*100);

第3种 利用random类来获取随机数
通过random类来获取随机数。
使用方法如下:
(01) 创建random对象。有两种方法可以创建random对象,如下:

random random = new random();//默认构造方法
random random = new random(1000);//指定种子数字

(02) 通过random对象获取随机数。random支持的随机值类型包括:boolean, byte, int, long, float, double。
比如,获取[0, 100)之间的int整数。方法如下:

int i2 = random.nextint(100);

random 的函数接口

// 构造函数(一): 创建一个新的随机数生成器。 


random() 
// 构造函数(二): 使用单个 long 种子创建一个新随机数生成器: public random(long seed) { setseed(seed); } next 方法使用它来保存随机数生成器的状态。
random(long seed) 

boolean nextboolean()     // 返回下一个“boolean类型”伪随机数。 
void  nextbytes(byte[] buf) // 生成随机字节并将其置于字节数组buf中。 
double nextdouble()     // 返回一个“[0.0, 1.0) 之间的double类型”的随机数。 
float  nextfloat()      // 返回一个“[0.0, 1.0) 之间的float类型”的随机数。 
int   nextint()       // 返回下一个“int类型”随机数。 
int   nextint(int n)    // 返回一个“[0, n) 之间的int类型”的随机数。 
long  nextlong()      // 返回下一个“long类型”随机数。 



synchronized double nextgaussian()  // 返回下一个“double类型”的随机数,它是呈高斯(“正常地”)分布的 double 值,其平均值是 0.0,标准偏差是 1.0。 
synchronized void setseed(long seed) // 使用单个 long 种子设置此随机数生成器的种子。


获取随机数示例

eg1:
下面通过示例演示上面3种获取随机数的使用方法。 源码如下(randomtest.java):

import java.util.random;
import java.lang.math;

/**
 * java 的随机数测试程序。共3种获取随机数的方法:
 *  (01)、通过system.currenttimemillis()来获取一个当前时间毫秒数的long型数字。
 *  (02)、通过math.random()返回一个0到1之间的double值。
 *  (03)、通过random类来产生一个随机数,这个是专业的random工具类,功能强大。
 *
 * @author skywang
 * @email kuiwu-wang@163.com
 */
public class randomtest{

  public static void main(string args[]){

    // 通过system的currenttimemillis()返回随机数
    testsystemtimemillis();

    // 通过math的random()返回随机数
    testmathrandom();

    // 新建“种子为1000”的random对象,并通过该种子去测试random的api
    testrandomapis(new random(1000), " 1st random(1000)");
    testrandomapis(new random(1000), " 2nd random(1000)");
    // 新建“默认种子”的random对象,并通过该种子去测试random的api
    testrandomapis(new random(), " 1st random()");
    testrandomapis(new random(), " 2nd random()");
  }

  /**
   * 返回随机数-01:测试system的currenttimemillis()
   */
  private static void testsystemtimemillis() {
    // 通过
    final long l = system.currenttimemillis();
    // 通过l获取一个[0, 100)之间的整数
    final int i = (int)( l % 100 );

    system.out.printf("\n---- system.currenttimemillis() ----\n l=%s i=%s\n", l, i);
  }


  /**
   * 返回随机数-02:测试math的random()
   */
  private static void testmathrandom() {
    // 通过math的random()函数返回一个double类型随机数,范围[0.0, 1.0)
    final double d = math.random();
    // 通过d获取一个[0, 100)之间的整数
    final int i = (int)(d*100);

    system.out.printf("\n---- math.random() ----\n d=%s i=%s\n", d, i);
  }


  /**
   * 返回随机数-03:测试random的api
   */
  private static void testrandomapis(random random, string title) {
    final int buffer_len = 5;

    // 获取随机的boolean值
    boolean b = random.nextboolean();
    // 获取随机的数组buf[]
    byte[] buf = new byte[buffer_len];
    random.nextbytes(buf);
    // 获取随机的double值,范围[0.0, 1.0)
    double d = random.nextdouble();
    // 获取随机的float值,范围[0.0, 1.0)
    float f = random.nextfloat();
    // 获取随机的int值
    int i1 = random.nextint();
    // 获取随机的[0,100)之间的int值
    int i2 = random.nextint(100);
    // 获取随机的高斯分布的double值
    double g = random.nextgaussian();
    // 获取随机的long值
    long l = random.nextlong();

    system.out.printf("\n---- %s ----\nb=%s, d=%s, f=%s, i1=%s, i2=%s, g=%s, l=%s, buf=[",
        title, b, d, f, i1, i2, g, l);
    for (byte bt:buf) 
      system.out.printf("%s, ", bt);
    system.out.println("]");
  }
}

eg2:

问题:生成(-10,10)之间的保留小数点后两位数的随机数。
解决方法:
1.java中随机数生成函数random r=new random(); r.nextfloat();//生成(0,1)之间的浮点型随机数。将上述随机数乘以10,得到生成(0,10)之间的随机数。
2.生成一个boolean型的随机数用于控制数的正负:r.nextboolean();
3.保留小数位数两位的方法:math.floor(n*100+0.5)/100;得到的数为double型。
代码如下:
  

import java.util.*;
  public class createrandom {
   public float numrandom(){
    float num;
    random r=new random();
    float value = (float) (math.floor(r.nextfloat()*1000+0.5)/100);
    boolean b = r.nextboolean();
    if(b){
     num = value;
     }
    else{
     num=0-value;
     }
    return num;
    }
    public static void main(string[] args) {
     createrandom cr = new createrandom();
     float num = cr.numrandom();
     system.out.print(num);
    }
  }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网