返回列表 發帖

亂數 (四)

本帖最後由 tonyh 於 2012-10-8 21:13 編輯

產生20個範圍介於123~127的隨機整數亂數.
  1. public class ch22
  2. {
  3.   public static void main (String args[])
  4.   {
  5.       for(int i=1; i<=20; i++)
  6.       {
  7.          System.out.println((int)(Math.random()*5+123));  //產生介於123~127的隨機亂數
  8.       }
  9.   }
  10. }

  11. /*
  12.   System.out.println(Math.random());  //產生介於0.00000~0.999999(無窮小數)的隨機亂數
  13.   System.out.println(Math.random()*5);  //產生介於0.00000~4.999999(無窮小數)的隨機亂數
  14.   System.out.println(Math.random()*5+123);  //產生介於123.00000~127.999999(無窮小數)的隨機亂數
  15.   System.out.println(Math.random()*5+123);  //產生介於123.00000~127.999999(無窮小數)的隨機亂數
  16.   System.out.println((int)(Math.random()*5+123));  //產生介於123~127的隨機亂數
  17. */
複製代碼

返回列表