Link to home
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

randomize array of integers

I am trying to shuffle an array of 52 integers using the following method:

	public static List<Integer> shuffleArray(Integer[] arr) {
		
		Integer[] randomizedArray = new Integer[]{};
		
		for(Integer i=0; i<arr.size(); i++) {
			Double dbl = math.random() * arr.size();
			randomizedArray.add(dbl.intValue());
		}
		return randomizedArray;
	}

Open in new window


I want to randomize the array so that the integers are out of order and unique.  Right now, the array is randomized, but it is not unique.  I want all the integers to be unique.  I also want to make sure the order of the array is never 0,1,2,3,4,5,6,7...etc...51.

I'm using the Salesforce apex language above, which is a derivative of Java.  I don't have access to the shuffle method and the random object.  I need to accomplish it with the above code structure.

Any help is appreciated.
Thanks.
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of -Dman100-

ASKER

Thanks for responding to my post.  I tried the following:

	public static List<Integer> shuffleArray(Integer[] arr) {
		
		Integer[] randomizedArray = new Integer[]{};
		Boolean[] chooseArray = new Boolean[]{};
		
		for(Integer i=0; i<arr.size(); i++) {
			chooseArray.add(false);
		}
		
		for(Integer i=0; i<arr.size();) {
			Double dbl = math.random() * arr.size();
			Integer intgr = dbl.intValue();
			if(!chooseArray[intgr]) {
				randomizedArray.add(dbl.intValue());
				chooseArray[intgr] = true;
				i++;
			}
			
		}
		return randomizedArray;
	}
	
	static testMethod void testShuffleArray() {
		Integer[] originalArray = new Integer[]{};
		for(Integer i=0; i<52; i++) {
			originalArray.add(i);
		}
		
		test.startTest();
			Integer[] shuffledArray = BlueWolfCodingExample.shuffleArray(originalArray);
		test.stopTest();
		
		system.debug('********************* shuffledArray size ' + shuffledArray.size());
		for(Integer j=0; j<shuffledArray.size(); j++) {
			system.debug('******************* ' + shuffledArray[j]);
			system.assertNotEquals(originalArray[j], shuffledArray[j]);
		}
	}

Open in new window


This is does work most of times, but I'm noticing my unit test fails occassionly because an index in the original list can equal an index in the randomized list.

Perhaps there is a better way to test that the new list is randomized?  Suggestions?

In my test, I just want to show that the array has been randomized.

Thanks for the help!  I appreciate it!
Regards.
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Thank you!
Once you've done this for every element in the array, you can be sure that it is shuffled,

No you can't be sure. The loop index could map to any of the selected random number(s), and so no swap would take place.