Java配列をランダム化
2138 ワード
import java.util.Date;
import java.util.Random;
/*
* , , ,
*/
public class Main {
public static void main(String args[]){
int data[]=new int[]{1,42,51,62,8,94,23,13,40,5};
//int p[]=getRandom(1,-8,100);
//show(p);
show(data);
permuteBySort(data);
show(data);
randomizeInPlace(data);
show(data);
}
/*
* , , A[i] P[i],
*
*/
private static void permuteBySort(int[] data)
{
int len=data.length;
int len3=len*len*len;
int P[]=getRandom(1,len3,len);
//
for(int i=len-1; i>0; i--)
{
for(int j=0; jP[j+1])
{
int temp=data[j];
data[j]=data[j+1];
data[j+1]=temp;
temp=P[j];
P[j]=P[j+1];
P[j+1]=temp;
}
}
}
}
/*
* A[i] A[i] A[n]
*/
private static void randomizeInPlace(int[] data)
{
Date dt=new Date();
Random random=new Random(dt.getSeconds());
int len=data.length;
for(int i=0; ib)
{
int temp=a;
a=b;
b=temp;
}
Date dt=new Date();
Random random=new Random(dt.getSeconds());
int res[]=new int[n];
for(int i=0; i
/**
*
* @author noam
*/
public class NRandom {
/**
* 0 1
* @param no
* @return
*/
public static int[] getSequence(int no) {
int[] sequence = new int[no];
for(int i = 0; i < no; i++){
sequence[i] = i;
}
Random random = new Random();
for(int i = 0; i < no; i++){
int p = random.nextInt(no);
int tmp = sequence[i];
sequence[i] = sequence[p];
sequence[p] = tmp;
}
random = null;
return sequence;
}