import java.util.Random; /* An instance of this class (which has no instance variables) has a ** method that produces an array containing pseudo-randomly generated ** integers. */ public class RandIntArrayMaker { /* Returns an array of the specified length containing pseudo-randomly ** generated integers in the specified range [bottom..top] using the ** given Random object. ** pre: length >= 0, bottom <= top, rand != null */ public int[] randomIntArray(int length, int bottom, int top, Random rand) { int[] result = new int[length]; int topBotDiff = top - bottom + 1; for (int i=0; i != result.length; i++) { result[i] = bottom + rand.nextInt(topBotDiff); } return result; } /* Returns an array of the specified length containing pseudo-randomly ** generated integers in the specified range [bottom..top] using a ** Random object created using the given seed. ** pre: length >= 0, bottom <= top */ public int[] randomIntArray(int length, int bottom, int top, int seed) { return randomIntArray(length, bottom, top, new Random(seed)); } /* Returns an array of the specified length containing pseudo-randomly ** generated Integer objects in the specified range [bottom..top] using ** the given Random object. ** pre: length >= 0, bottom <= top, rand != null */ public Integer[] randomIntegerArray(int length, int bottom, int top, Random rand) { return intArrayToIntegerArray(randomIntArray(length, bottom, top, rand)); } /* Returns an array of the specified length containing pseudo-randomly ** generated Integer objects in the specified range [bottom..top] using a ** Random object created using the given seed. ** pre: length >= 0, bottom <= top */ public Integer[] randomIntegerArray(int length, int bottom, int top, int seed) { return randomIntegerArray(length, bottom, top, new Random(seed)); } // private // ------- /* Returns a duplicate of the given array, except that each element is of ** type Integer rather than int. */ private Integer[] intArrayToIntegerArray(int[] a) { int N = a.length; Integer[] result = new Integer[N]; for (int i=0; i != N; i++) { result[i] = a[i]; } // exploits autoboxing return result; } }