Skip to content

Instantly share code, notes, and snippets.

@ayato-p
Created February 13, 2012 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayato-p/1812959 to your computer and use it in GitHub Desktop.
Save ayato-p/1812959 to your computer and use it in GitHub Desktop.
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