Solved

List<> shuffle

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

12 Experts available now in Live!

Get 1:1 Help Now