|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| RandomChooser.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
package baseCode.math;
|
|
| 2 |
|
|
| 3 |
import java.util.Random;
|
|
| 4 |
|
|
| 5 |
/**
|
|
| 6 |
* Fill arrays with random values given a source of values.
|
|
| 7 |
* <p>
|
|
| 8 |
* Copyright (c) 2004
|
|
| 9 |
* </p>
|
|
| 10 |
* <p>
|
|
| 11 |
* Institution: Columbia University
|
|
| 12 |
* </p>
|
|
| 13 |
*
|
|
| 14 |
* @author Paul Pavlidis
|
|
| 15 |
* @version $Id: RandomChooser.java,v 1.8 2005/03/21 18:01:04 pavlidis Exp $
|
|
| 16 |
*/
|
|
| 17 |
public class RandomChooser { |
|
| 18 |
|
|
| 19 | 0 |
private RandomChooser() { /* block instantiation */ |
| 20 |
} |
|
| 21 |
|
|
| 22 |
private static Random generator = new Random( System.currentTimeMillis() ); |
|
| 23 |
|
|
| 24 |
/**
|
|
| 25 |
* Initialized the random number generator witha given seed.
|
|
| 26 |
* @param seed
|
|
| 27 |
*/
|
|
| 28 | 0 |
public static void init(long seed) { |
| 29 | 0 |
generator = new Random(seed);
|
| 30 |
} |
|
| 31 |
|
|
| 32 |
/**
|
|
| 33 |
* Fill randomvals with random things from sourcedata, without replacement.
|
|
| 34 |
*
|
|
| 35 |
* @param randomvals answers go here.
|
|
| 36 |
* @param sourcedata Data to be randomly selected
|
|
| 37 |
* @param deck an array pre-filled with integers from 0 to max, but they don't have to be in order.
|
|
| 38 |
* @param max how many values we need.
|
|
| 39 |
* @param n int
|
|
| 40 |
*/
|
|
| 41 | 0 |
public static void chooserandom( double[] randomvals, double[] sourcedata, |
| 42 |
int[] deck, int max, int n ) { |
|
| 43 | 0 |
int rand;
|
| 44 | 0 |
int i;
|
| 45 | 0 |
int temp;
|
| 46 | 0 |
for ( i = 0; i < n; i++ ) {
|
| 47 | 0 |
rand = generator.nextInt( max - i ) + i; // a value between i and max.
|
| 48 | 0 |
temp = deck[rand]; |
| 49 | 0 |
deck[rand] = deck[i]; |
| 50 | 0 |
deck[i] = temp; |
| 51 | 0 |
randomvals[i] = sourcedata[temp]; |
| 52 |
} |
|
| 53 |
} |
|
| 54 |
|
|
| 55 |
/**
|
|
| 56 |
* choose n random integers from 0 to max without repeating
|
|
| 57 |
*
|
|
| 58 |
* @param randomnums answers go here.
|
|
| 59 |
* @param deck an array pre-filled with integers from 0 to max, but they don't have to be in order.
|
|
| 60 |
* @param max how many values we need.
|
|
| 61 |
* @param n int
|
|
| 62 |
*/
|
|
| 63 | 0 |
public static void chooserandom( int[] randomnums, int[] deck, int max, int n ) { |
| 64 | 0 |
int rand;
|
| 65 | 0 |
int i;
|
| 66 | 0 |
for ( i = 0; i < n; i++ ) {
|
| 67 | 0 |
rand = generator.nextInt( max - i ) + i; // a value between i and max.
|
| 68 | 0 |
randomnums[i] = deck[rand]; |
| 69 | 0 |
deck[rand] = deck[i]; |
| 70 | 0 |
deck[i] = randomnums[i]; |
| 71 |
} |
|
| 72 |
} |
|
| 73 |
|
|
| 74 |
/**
|
|
| 75 |
* choose n random integers from 0 to max without repeating
|
|
| 76 |
*
|
|
| 77 |
* @param randomnums int[]
|
|
| 78 |
* @param recLog record of what values are already chosen.
|
|
| 79 |
* @param max int
|
|
| 80 |
* @param n int
|
|
| 81 |
*/
|
|
| 82 | 0 |
public static void chooserandom( int[] randomnums, boolean[] recLog, |
| 83 |
int max, int n ) { |
|
| 84 | 0 |
int numgot;
|
| 85 | 0 |
int i;
|
| 86 | 0 |
int newnum;
|
| 87 |
|
|
| 88 | 0 |
numgot = 0; |
| 89 |
|
|
| 90 | 0 |
while ( numgot < n ) { /* numgot is the index of the last gotten item */ |
| 91 | 0 |
newnum = generator.nextInt( max ); |
| 92 | 0 |
if ( !recLog[newnum] ) {
|
| 93 | 0 |
randomnums[numgot] = newnum; |
| 94 | 0 |
recLog[newnum] = true;
|
| 95 | 0 |
numgot++; |
| 96 |
} |
|
| 97 |
} |
|
| 98 |
|
|
| 99 |
// reset all elements in recLog to false
|
|
| 100 | 0 |
for ( i = 0; i < n; i++ ) {
|
| 101 | 0 |
recLog[randomnums[i]] = false;
|
| 102 |
} |
|
| 103 |
|
|
| 104 |
} |
|
| 105 |
|
|
| 106 |
/**
|
|
| 107 |
* Same as chooserandom, but with replacement -- that is, repeats are allowed.
|
|
| 108 |
*
|
|
| 109 |
* @param randomnums int[]
|
|
| 110 |
* @param max int
|
|
| 111 |
* @param n int
|
|
| 112 |
*/
|
|
| 113 | 0 |
public static void chooserandomWrep( int[] randomnums, int max, int n ) { |
| 114 | 0 |
for ( int i = 0; i < n; i++ ) { |
| 115 | 0 |
int newnum = ( char ) ( generator.nextInt() % max ); |
| 116 | 0 |
randomnums[i] = newnum; |
| 117 |
} |
|
| 118 |
} |
|
| 119 |
|
|
| 120 |
} |
|
||||||||||