我想介绍看到的两种产生随机数组的方法,伪代码如下:
第一种:优先级排序法:
PERMUTE-BY-SORTING(A) 1 n = length[A]; 2 for i = 1 to n 3 do P[i] = RANDOM(1,n*n*n); 4 sort A, usng P as sort keys 5 return A;
第二种:下标交换法
RANDOMIZE-IN-PLACE(A) 1 n = length[A]; 2 for i = 1 to n 3 do swap(A[i],A[RANDOM(i,n)])