To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Article :: Building Dynamic Systems with Expressions in .NET
How Is XML Like An Interface?
Understanding Covariance and Contravariance
Print VS 2010 Keyboard Shortcut References in Letter (8.5x11in) and A4 (210×297mm) Sizes
Updated Productivity Power Tools



Go Back   VBForums > VBForums CodeBank > CodeBank - C#

Reply Post New Thread
 
Thread Tools Display Modes
Old Sep 29th, 2009, 08:25 AM   #1
jmcilhinney
.NUT
 
jmcilhinney's Avatar
 
Join Date: May 05
Location: Sydney, Australia
Posts: 60,536
jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)
(.NET 3.5) Randomise a List of Items

VB version here.

The following is an extension method for getting the contents of any IEnumerable(Of T) object, e.g. an array or List(Of T), in random order:
Code:
using System;
using System.Collections.Generic;
using System.Linq;

public static class Enumerable
{
    public static IEnumerable<T> Randomise<T>(this IEnumerable<T> items)
    {
        T[] result = null;

        if (items != null)
        {
            var rng = new Random();

            result = (from item in items
                      orderby rng.NextDouble()
                      select item).ToArray();
        }

        return result;
    }
}
Note that it obeys the rules for extension methods, i.e. it's a member of a static class and the first argument is declared with the 'this' keyword. You might use it something like this:
Code:
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};

foreach (var number in numbers.Randomise())
{
    MessageBox.Show(number.ToString());
}
Try running that repeatedly and you'll get a different order each time.

Note that you could write a regular method that does the same thing in earlier versions.
__________________

2007, 2008, 2009, 2010

Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#)
My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET

Last edited by jmcilhinney; Sep 29th, 2009 at 08:57 PM. Reason: Simplified code
jmcilhinney is offline   Reply With Quote
Old Sep 29th, 2009, 08:56 PM   #2
jmcilhinney
.NUT
 
jmcilhinney's Avatar
 
Join Date: May 05
Location: Sydney, Australia
Posts: 60,536
jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)
Re: (.NET 3.5) Randomise a List of Items

Note that I have simplified the code a little from the original. The new code will not throw an exception if you call it on a null reference but it will return a null reference. As such, an exception will be thrown if you try to enumerate the result, which did not happen with the old code. You could easily adjust the code to return an empty array if a null reference is passed in to avoid that if you think it's appropriate.
__________________

2007, 2008, 2009, 2010

Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#)
My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET
jmcilhinney is offline   Reply With Quote
Old Oct 4th, 2009, 06:26 PM   #3
DeanMc
Frenzied Member
 
DeanMc's Avatar
 
Join Date: Jul 08
Location: Rep of Ireland
Posts: 1,339
DeanMc has a spectacular aura about (150+)DeanMc has a spectacular aura about (150+)
Re: (.NET 3.5) Randomise a List of Items

Very nice use of extension methods jm!
__________________
Latest Photo's:


Current Project: Project Audiophile
DeanMc is offline   Reply With Quote
Reply

Go Back   VBForums > VBForums CodeBank > CodeBank - C#


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:05 PM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.