生成
package mon
import java
import java
/**
*
*/
public class RandomNumberGenerator {
/**
* 這是典型的隨機洗牌算法
* 流程是從備選數組中選擇一個放入目標數組中
* 算法時間復雜度O(n)
* @return 隨機
*/
public static String generateNumber() {
String no=
//初始化備選數組
int[] defaultNums = new int[
for (int i =
defaultNums[i] = i;
}
Random random = new Random()
int[] nums = new int[LENGTH];
//默認數組中可以選擇的部分長度
int canBeUsed =
//填充目標數組
for (int i =
//將隨機選取的數字存入目標數組
int index = random
nums[i] = defaultNums[index];
//將已用過的數字扔到備選數組最後
swap(index
canBeUsed
}
if (nums
for (int i =
no+=nums[i];
}
}
return no;
}
private static final int LENGTH =
private static void swap(int i
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public static String generateNumber
String no=
int num[]=new int[
int c=
for (int i =
num[i] = new Random()
c = num[i];
for (int j =
if (num[j] == c) {
i
break;
}
}
}
if (num
for (int i =
no+=num[i];
}
}
return no;
}
public static void main(String[] args) {
for (int i =
// System
System
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26871.html