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.
JavaSalesforce

Avatar of undefined
Last Comment
krakatoa

8/22/2022 - Mon
SOLUTION
Dave Baldwin

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Peter Kwan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
-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
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
-Dman100-

ASKER
Thank you!
krakatoa

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck