1. 程式人生 > >java學習之產生隨機數的三種方式

java學習之產生隨機數的三種方式

  隨機數在實際中使用很廣泛,比如要隨即生成一個固定長度的字串、數字。或者隨即生成一個不定長度的數字、或者進行一個模擬的隨機選擇等等。Java提供了最基本的工具,可以幫助開發者來實現這一切。

  一、Java隨機數的產生方式

  在Java中,隨機數的概念從廣義上將,有三種。

  1、通過System.currentTimeMillis()來獲取一個當前時間毫秒數的long型數字。

  2、通過Math.random()返回一個0到1之間的double值。

  3、通過Random類來產生一個隨機數,這個是專業的Random工具類,功能強大。

  二、Random類API說明

  1、Java API說明

  Random類的例項用於生成偽隨機數流。此類使用 48 位的種子,使用線性同餘公式對其進行修改(請參閱 Donald Knuth 的《The Art of Computer Programming, Volume 2》,第 3.2.1 節)。

  如果用相同的種子建立兩個 Random 例項,則對每個例項進行相同的方法呼叫序列,它們將生成並返回相同的數字序列。為了保證屬性的實現,為類 Random 指定了特定的演算法。

  很多應用程式會發現 Math 類中的 random 方法更易於使用。

  2、方法摘要

  Random()

  建立一個新的隨機數生成器。

  Random(long seed)

  使用單個 long 種子建立一個新隨機數生成器: public Random(long seed) { setSeed(seed); } next 方法使用它來儲存隨機數生成器的狀態。

  protected int next(int bits)

  生成下一個偽隨機數。

  boolean nextBoolean()

  返回下一個偽隨機數,它是從此隨機數生成器的序列中取出的、均勻分佈的 boolean 值。

  void nextBytes(byte[] bytes)

  生成隨機位元組並將其置於使用者提供的位元組陣列中。

  double nextDouble()

  返回下一個偽隨機數,它是從此隨機數生成器的序列中取出的、在 0.0 和 1.0之間均勻分佈的 double 值。

  float nextFloat()

  返回下一個偽隨機數,它是從此隨機數生成器的序列中取出的、在 0.0 和 1.0 之間均勻分佈的 float 值。

  double nextGaussian()

  返回下一個偽隨機數,它是從此隨機數生成器的序列中取出的、呈高斯(“正常地”)分佈的 double 值,其平均值是 0.0,標準偏差是 1.0。

  int nextInt()

  返回下一個偽隨機數,它是此隨機數生成器的序列中均勻分佈的 int 值。

  int nextInt(int n)

  返回一個偽隨機數,它是從此隨機數生成器的序列中取出的、在 0(包括)和指定值(不包括)之間均勻分佈的 int值。

  long nextLong()

  返回下一個偽隨機數,它是從此隨機數生成器的序列中取出的、均勻分佈的 long 值。

  void setSeed(long seed)

  使用單個 long 種子設定此隨機數生成器的種子。

  三、Random類使用說明

  1、帶種子與不帶種子的區別Random類使用的根本是策略分帶種子和不帶種子的Random的例項。

  通俗說,兩者的區別是:帶種子的,每次執行生成的結果都是一樣的。

  不帶種子的,每次執行生成的都是隨機的,沒有規律可言。

  2、建立不帶種子的Random物件

  Random random = new Random();

  3、建立不帶種子的Random物件有兩種方法:

  1) Random random = new Random(555L);

  2) Random random = new Random();random.setSeed(555L);

  四、測試

  通過一個例子說明上面的用法

  import java.util.Random;

  public class TestRandomNum {

  public static void main(String[] args) {

  randomTest();

  testNoSeed();

  testSeed1();

  testSeed2();

  }

  public static void randomTest() {

  System.out.println("--------------test()--------------");

  //返回以毫秒為單位的當前時間。

  long r1 = System.currentTimeMillis();

  //返回帶正號的 double 值,大於或等於 0.0,小於 1.0。

  double r2 = Math.random();

  //通過Random類來獲取下一個隨機的整數

  int r3 = new Random().nextInt();

  System.out.println("r1 = " + r1);

  System.out.println("r3 = " + r2);

  System.out.println("r2 = " + r3);

  }

  public static void testNoSeed() {

  System.out.println("--------------testNoSeed()--------------");

  //建立不帶種子的測試Random物件

  Random random = new Random();

  for (int i = 0; i < 3; i++) {

  System.out.println(random.nextInt());

  }

  }

  public static void testSeed1() {

  System.out.println("--------------testSeed1()--------------");

  //建立帶種子的測試Random物件

  Random random = new Random(555L);

  for (int i = 0; i < 3; i++) {

  System.out.println(random.nextInt());

  }

  }

  public static void testSeed2() {

  System.out.println("--------------testSeed2()--------------");

  //建立帶種子的測試Random物件

  Random random = new Random();

  random.setSeed(555L);

  for (int i = 0; i < 3; i++) {

  System.out.println(random.nextInt());

  }

  }

  }

  執行結果:

  --------------test()--------------

  r1 = 1227108626582

  r3 = 0.5324887850155043

  r2 = -368083737

  --------------testNoSeed()--------------

  809503475

  1585541532

  -645134204

  --------------testSeed1()--------------

  -1367481220

  292886146

  -1462441651

  --------------testSeed2()--------------

  -1367481220

  292886146

  -1462441651

  Process finished with exit code 0

  通過testSeed1()與testSeed2()方法的結果可以看到,兩個列印結果相同,因為他們種子相同,再執行一次,結果還是一樣的,這就是帶種子隨機數的特性。

  而不帶種子的,每次執行結果都是隨機的。

  五、綜合應用

  下面通過最近寫的一個隨機數工具類來展示用法:

  import java.util.Random;

  public class RandomUtils {

  public static final String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

  public static final String letterChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

  public static final String numberChar = "0123456789";

  public static String generateString(int length) {

  StringBuffer sb = new StringBuffer();

  Random random = new Random();

  for (int i = 0; i < length; i++) {

  sb.append(allChar.charAt(random.nextInt(allChar.length())));

  }

  return sb.toString();

  }

  public static String generateMixString(int length) {

  StringBuffer sb = new StringBuffer();

  Random random = new Random();

  for (int i = 0; i < length; i++) {

  sb.append(allChar.charAt(random.nextInt(letterChar.length())));

  }

  return sb.toString();

  }

  public static String generateLowerString(int length) {

  return generateMixString(length).toLowerCase();

  }

  public static String generateUpperString(int length) {

  return generateMixString(length).toUpperCase();

  }

  public static String generateZeroString(int length) {

  StringBuffer sb = new StringBuffer();

  for (int i = 0; i < length; i++) {

  sb.append('0');

  }

  return sb.toString();

  }

  public static String toFixdLengthString(long num, int fixdlenth) {

  StringBuffer sb = new StringBuffer();

  String strNum = String.valueOf(num);

  if (fixdlenth - strNum.length() >= 0) {

  sb.append(generateZeroString(fixdlenth - strNum.length()));

  } else {

  throw new RuntimeException("將數字" + num + "轉化為長度為" + fixdlenth + "的字串發生異常!");

  }

  sb.append(strNum);

  return sb.toString();

  }

  public static String toFixdLengthString(int num, int fixdlenth) {

  StringBuffer sb = new StringBuffer();

  String strNum = String.valueOf(num);

  if (fixdlenth - strNum.length() >= 0) {

  sb.append(generateZeroString(fixdlenth - strNum.length()));

  } else {

  throw new RuntimeException("將數字" + num + "轉化為長度為" + fixdlenth + "的字串發生異常!");

  }

  sb.append(strNum);

  return sb.toString();

  }

  public static void main(String[] args) {

  System.out.println(generateString(15));

  System.out.println(generateMixString(15));

  System.out.println(generateLowerString(15));

  System.out.println(generateUpperString(15));

  System.out.println(generateZeroString(15));

  System.out.println(toFixdLengthString(123, 15));

  System.out.println(toFixdLengthString(123L, 15));

  }

  }

  執行結果:

  vWMBPiNbzfGCpHG

  23hyraHdJkKPwMv

  tigowetbwkm1nde

  BPZ1KNEJPHB115N

  000000000000000

  000000000000123

  000000000000123

  Process finished with exit code 0

  六、總結

  1、隨機數很常用,在Java有三種產生方式,以Random隨機數的使用最為複雜。

  2、Random類物件有是否帶種子之分,帶種子的只要種子相同,多次執行,生成隨機數的結果總是那樣。

  3、帶種子隨機數的帶種子的物件建立方式有兩種,效果一樣。但是帶種子的隨機數用處似乎不大。

  4、Random的功能涵蓋了Math.random()的功能。

  5、可以通過隨機數去做實現隨機字串等複雜的隨機資料。

  6、不要研究不重複的隨機數,意義不大。

在Java 中我們可以使用java.util.Random類來產生一個隨機數發生器。它有兩種形式的建構函式,分別是Random()和Random(long seed)。Random()使用當前時間即System.currentTimeMillis()作為發生器的種子,Random(long seed)使用指定的seed作為發生器的種子。

        隨機數發生器(Random)物件產生以後,通過呼叫不同的method:nextInt()、nextLong()、nextFloat()、nextDouble()等獲得不同型別隨機數。

       1>生成隨機數
           Random random = new Random();
           Random random = new Random(100);//指定種子數100
           random呼叫不同的方法,獲得隨機數。
           如果2個Random物件使用相同的種子(比如都是100),並且以相同的順序呼叫相同的函式,那它們返回值完全相同。如下面程式碼中兩個Random物件的輸出完全相同
          import java.util.*;
          class TestRandom {
                public static void main(String[] args) {
                     Random random1 = new Random(100);
                     System.out.println(random1.nextInt());
                     System.out.println(random1.nextFloat());
                     System.out.println(random1.nextBoolean());
                     Random random2 = new Random(100);
                     System.out.println(random2.nextInt());
                     System.out.println(random2.nextFloat());
                     System.out.println(random2.nextBoolean());