Created
February 13, 2012 02:55
-
-
Save ayato-p/1812959 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.Random; | |
public class RandomCreate { | |
private int[] bitArray; | |
/** | |
* overLoad | |
* @param max | |
* @return randCreate(0, max) | |
*/ | |
public long randCreate(int max){ | |
return randCreate(0, max); | |
} | |
/** | |
* The number generated at random in the range of min to max is returned. | |
* @param min | |
* @param max | |
* @return result | |
*/ | |
public int randCreate(int min, int max){ | |
Random rand = new Random(); | |
int result = rand.nextInt(max-min) + min; | |
return result; | |
} | |
public int[] CreateRandomArray(int n, int k){ | |
bitArray = new int[n]; | |
for(int i=0; i<n; i++){ | |
bitArray[i] = i; | |
} | |
for(int i=0; i<n; i++){ | |
swapArray(i, randCreate(i, n)); | |
} | |
int[] bit = new int[k]; | |
for(int i=0; i<k; i++){ | |
bit[i] = bitArray[i]; | |
} | |
return bit; | |
} | |
private void swapArray(int from, int to){ | |
int t = bitArray[from]; | |
bitArray[from] = bitArray[to]; | |
bitArray[to] = t; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment