Solved

List<> shuffle

Posted on 2009-05-12
2
566 Views
Last Modified: 2013-12-17
I have a Generic List (List<>). How is best to shuffle it? (randomize its items)
0
Comment
Question by:johnkainn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 24366491
maybe add a property on your object that is a random number and then sort the list by that.
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 250 total points
ID: 24366613
that would take the time of the sort ...

you could randomize it in linear time by just removing/adding the node at a random spot.

for(int i=0;i<list.length;i++) {
int newIndex = (int) Random.Next * SizeOfList - 1;
list.RemoveAt(i);
list.InsertAt(newIndex);
}

Cheers,

Greg
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

736 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question