randomize a list of search results

I'm trying to randomize a list of search results returned from a query using APEX on the Salesforce platform. The closest language to APEX is Java so I added this to the Java topic in hopes a java expert could help.  Right now, the same results are returned in the same order when using a specific search algorithm. It returns the list in the same order every time and the user sees the first series of records in the UI. I want to randomize that list so the first series of records will be different each time the user searches using the same search algorithm.

I created a method to return a random integer based on the lower and upper bounds of the search results list:

public static Integer getRandomInt (Integer lower, Integer upper)
	return (Integer) (Math.floor(Math.random() * (upper - lower)) + lower);

Open in new window

I'm stuck on how I can set each element in the list with the random integer as the index until all elements have been set and I don't want any indexes to repeat.  So, if I return 100 results back, as I iterate of the list, a random integer is generated, the index of that list element is set with that integer value within the upper and lower bounds, then the next iteration occurs, checks if the next integer has not already been used and then sets the index for the next list element. This continues until all list elements have been set. If I'm thinking the logic thru correctly, I should have a completely random list of search results.  

How can I accomplish this using the Salesforce APEX language?  Is there a better way to do this? Salesforce does not have a method to randomize a list or keyword to return a soql query in a random order, so the only way I can think to do this is using the math functions and create a new list and randomly setting each element index.

Any help is greatly appreciated.
-Dman100-Software ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
I don't know Salesforce but I do know Java. Can you show us the code where you get the list of results, and I might be able to give you a hint in the right direction?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

is a way to get and shuffle your indexes in Java. For randomising your hundred results, you would call

int[] resultIndexes = nRandomsFromRange(100, 0, 99);

Open in new window

Can you please tell me why you marked an offer of help as an accepted answer rather than an actually helpful answer?
-Dman100-Software ConsultantAuthor Commented:
Well, I wanted to split the points to both people that responded to my post. I did a lot of research into options to randomize results in APEX and there are some fundamental limitations that is not an issue in Java. Since you both offered help, I wanted to extend points to both of you. I certainly did not think this would be an issue?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Cloud Computing

From novice to tech pro — start learning today.