In this example, the getPermutations()
and getSampleIndices()
methods are used to sample ten values from a hypothetical vector of 1,000,000 values.
import com.imsl.stat.*; import com.imsl.math.PrintMatrix; public class RandomSamplesEx4 { public static void main(String[] args) { int nsamp = 10; int xlength = 1000000; RandomSamples rs = new RandomSamples(); Random r = new Random(123457L); r.setMultiplier(16807); rs.setRandomObject(r); int[] idx = rs.getSampleIndices(nsamp, xlength); int[] perm = rs.getPermutation(nsamp); int[] samp = new int[nsamp]; for (int i = 0; i < nsamp; i++) { samp[i] = idx[perm[i] - 1]; } PrintMatrix pm = new PrintMatrix("Random Sample"); pm.print(samp); } }
Random Sample 0 0 434,660 1 580,208 2 514,608 3 908,824 4 376,662 5 300,775 6 330,074 7 36,541 8 439,379 9 957,579Link to Java source.