import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class PrimeNumberTest {
/** Creates a new instance of PrimeNumberTest */
public PrimeNumberTest() { }
public static void main(String[] args){
//獲得一個4位數的隨機大素數
long longVar4 = createRadomPrimeNunber(4);
System.out.println(longVar4);
//獲得一個5位數的隨機大素數
long longVar5 = createRadomPrimeNunber(5);
System.out.println(longVar5);
}
public static long createRadomPrimeNunber(int n){
long recLong = 0;
List list = listAllPrimeNumber(n);
Random rd = new Random();
int randomIndex = Math.abs( rd.nextInt()%list.size());
recLong = ((Long)list.get(randomIndex)).longValue();
return recLong;
}
public static List listAllPrimeNumber(int n){
List list = new ArrayList();
long low = (long)Math.pow(10,n-1);
long high = (long)Math.pow(10,n) - 1;
for(long i= low;i < high;i++){
if( isSushu(i)) {
list.add(new Long(i));
} }
return list;
}
public static boolean isSushu(long x){
//定義一個判斷一個數是否是素數的函數
if(x<2) return false; if( x==2)return true;
for(long i=2;
i<= (long)Math.sqrt(x);i++)
if(x%i==0)
return false;
return true;
}}
例二
import java.util.Random;
/**
* Java實用工具類庫中的類java.util.Random提供了產生各種類型隨機數的方法。
* 它可以產生int、long、float、double以及Goussian等類型的隨機數。
* java.lang.Math中的方法random()只產生double型的隨機數。
*/
public class RandomNumber{
public static void main(String[] args) {
// 使用java.lang.Math的random方法生成隨機數
System.out.println("Math.random(): " + Math.random());
// 使用不帶參數的構造方法構造java.util.Random對象
System.out.println("使用不帶參數的構造方法構造的Random對象:");
Random rd1 = new Random();
// 產生各種類型的隨機數
// 按均勻分布產生整數
System.out.println("int: " + rd1.nextInt());
// 按均勻分布產生長整數
System.out.println("long: " + rd1.nextLong());
// 按均勻分布產生大于等于0,小于1的float數[0, 1)
System.out.println("float: " + rd1.nextFloat());
// 按均勻分布產生[0, 1)范圍的double數
System.out.println("double: " + rd1.nextDouble());
// 按正態分布產生隨機數
System.out.println("Gaussian: " + rd1.nextGaussian());
// 生成一系列隨機數
System.out.print("隨機整數序列:");
for (int i = 0; i < 5; i++) {
System.out.print(rd1.nextInt() + " ");
}
System.out.println();
// 指定隨機數產生的范圍
System.out.print("[0,10)范圍內隨機整數序列: ");
for (int i = 0; i < 10; i++) {
// Random的nextInt(int n)方法返回一個[0, n)范圍內的隨機數
System.out.print(rd1.nextInt(10) + " ");
}
System.out.println();
System.out.print("[5,23)范圍內隨機整數序列: ");
for (int i = 0; i < 10; i++) {
// 因為nextInt(int n)方法的范圍是從0開始的,
// 所以需要把區間[5,28)轉換成5 + [0, 23)。
System.out.print(5 + rd1.nextInt(23) + " ");
}
System.out.println();
System.out.print("利用nextFloat()生成[0,99)范圍內的隨機整數序列: ");
for (int i = 0; i < 10; i++) {
System.out.print((int) (rd1.nextFloat() * 100) + " ");
}
System.out.println();
System.out.println();
// 使用帶參數的構造方法構造Random對象
// 構造函數的參數是long類型,是生成隨機數的種子。
System.out.println("使用帶參數的構造方法構造的Random對象:");
Random ran2 = new Random(10);
// 對于種子相同的Random對象,生成的隨機數序列是一樣的。
System.out.println("使用種子為10的Random對象生成[0,10)內隨機整數序列: ");
for (int i = 0; i < 10; i++) {
System.out.print(ran2.nextInt(10) + " ");
}
System.out.println();
Random ran3 = new Random(10);
System.out.println("使用另一個種子為10的Random對象生成[0,10)內隨機整數序列: ");
for (int i = 0; i < 10; i++) {
System.out.print(ran3.nextInt(10) + " ");
}
System.out.println();
// ran2和ran3生成的隨機數序列是一樣的,如果使用兩個沒帶參數構造函數生成的Random對象,
// 則不會出現這種情況,這是因為在沒帶參數構造函數生成的Random對象的種子缺省是當前系統時間的毫秒數。
// 另外,直接使用Random無法避免生成重復的數字,如果需要生成不重復的隨機數序列,需要借助數組和集合類
//本書第4章將給出解決方法。
}
}
運行結果:
C:\>java RandomNumber
Math.random(): 0.525171492959965
使用不帶參數的構造方法構造的Random對象:
int: 636539740
long: -752663949229005813
float: 0.87349784
double: 0.4065973309853902
Gaussian: 0.4505871918488808
隨機整數序列:1936784917 1339857386 -1185229615 1883411721 1409219372
[0,10)范圍內隨機整數序列: 1 1 5 5 9 0 1 0 2 4
[5,23)范圍內隨機整數序列: 9 13 26 18 11 27 26 12 21 8
利用nextFloat()生成[0,99)范圍內的隨機整數序列: 1 47 72 59 49 86 80 88 55 82
使用帶參數的構造方法構造的Random對象:
使用種子為10的Random對象生成[0,10)內隨機整數序列:
3 0 3 0 6 6 7 8 1 4
使用另一個種子為10的Random對象生成[0,10)內隨機整數序列:
3 0 3 0 6 6 7 8 1 4
例三:帶隨機種子的隨機數
import java.util.*;
public class test {
public static void main(String[] args) {
Random rand = new Random();
for(int i = 0; i < 10; i++) {
rand.setSeed(i);
System.out.println(rand.nextInt());
}
}
}