Solved

List<> shuffle

Posted on 2009-05-12
2
547 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now