Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class RandomizeNumberArray3 {
- /**
- * Program: RandomizeNumberArray3.java
- * Purpose: Randomize order of array of contiguous integers
- * Creator: Chris Clarke
- * Created: 10.05.2015
- */
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int min = 0; // makes the range from 0 to max-1
- int max = 0;
- Integer[] unshuffled;
- Integer[] shuffled;
- System.out.println("*** Randomize Number Array ***");
- System.out.print("Enter size of the array: ");
- try {
- max = scan.nextInt();
- unshuffled = new Integer[max];
- } catch (Exception x) {
- System.out.println("Not a positive whole number!");
- return;
- } // end try/catch
- System.out.print("Enter initial value (0 or 1): ");
- try {
- min = scan.nextInt();
- } catch (Exception x) {
- System.out.println("Not a positive whole number!");
- return;
- } // end try/catch
- // initialise the array
- for (int i=0; i<max; i++)
- unshuffled[i] = i + min;
- // function call to randomize the array
- shuffled = randomize(unshuffled, min, max);
- System.out.println(Arrays.toString(shuffled));
- } // main()
- public static Integer[] randomize(Integer[] unshuffled, int min, int max) {
- // randomize the order of an array of linear values
- Integer[] shuffled;
- List<Integer> li = new ArrayList<Integer>();
- li = Arrays.asList(unshuffled);
- Collections.shuffle(li);
- shuffled = new Integer[max];
- for (int i=0; i<li.size(); i++)
- shuffled[i] = li.get(i);
- return shuffled;
- } // randomize()
- } // class RandomizeNumberArray3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement