![]() |
|
|
#1 |
|
Guest
Posts: n/a
|
Shuffle a Collection
Hi everyone,
I have to write a function to shuffle a collection. If anyone has any idea on how to implement it please share with me. The signature of function is: ICollection Shuffle(ICollection c); Thanks. AM |
|
|
|
#2 |
|
Guest
Posts: n/a
|
Re: Shuffle a Collection
Aamir Mahmood wrote:
> I have to write a function to shuffle a collection. If anyone has any idea > on how to implement it please share with me. > > The signature of function is: > ICollection Shuffle(ICollection c); 2.0+ style: public class Util<T> { private static Random rng = new Random(); public static ICollection<T> Shuffle(ICollection<T> c) { T[] a = new T[c.Count]; c.CopyTo(a, 0); byte[] b = new byte[a.Length]; rng.NextBytes(b); Array.Sort(b, a); return new List<T>(a); } } 1.x style: public class Util { private static Random rng = new Random(); public static ICollection Shuffle(ICollection c) { object[] a = new object[c.Count]; c.CopyTo(a, 0); byte[] b = new byte[a.Length]; rng.NextBytes(b); Array.Sort(b, a); return new ArrayList(a); } } Arne |
|
|
|
#3 |
|
Guest
Posts: n/a
|
Re: Shuffle a Collection
I have already implemented it, any comments/suggestions on my implementation
are welcome: ---------------------------------------------------------------------- public static ICollection Shuffle(ICollection c) { if (c == null || c.Count <= 1) { return c; } byte[] bytes = new byte[4]; RNGCryptoServiceProvider cRandom = new RNGCryptoServiceProvider(); cRandom.GetBytes(bytes); int seed = BitConverter.ToInt32(bytes, 0); Random random = new Random(seed); ArrayList orig = new ArrayList(c); ArrayList randomized = new ArrayList(c.Count); for (int i = 0; i < c.Count; i++) { int index = random.Next(orig.Count); randomized.Add(orig[index]); orig.RemoveAt(index); } return randomized; } ------------------------------------------------ Thanks. AM "Aamir Mahmood" <> wrote in message news:%23xjW$.gbl... > Hi everyone, > > I have to write a function to shuffle a collection. If anyone has any > idea on how to implement it please share with me. > > The signature of function is: > ICollection Shuffle(ICollection c); > > Thanks. > > AM > |
|
|
|
#4 |
|
Guest
Posts: n/a
|
Re: Shuffle a Collection
On Aug 30, 3:08 am, Arne Vajhøj <> wrote:
> Aamir Mahmood wrote: > > I have to write a function to shuffle a collection. If anyone has any idea > > on how to implement it please share with me. > > > The signature of function is: > > ICollection Shuffle(ICollection c); > > 2.0+ style: <snip> Shuffling by sorting is going to be (at best) O(n log n). It's fairly easy to do it as O(n)... admittedly it's more code, but for large collections it could be significant. Jon |
|
|
|
#5 |
|
Guest
Posts: n/a
|
Re: Shuffle a Collection
Jon Skeet [C# MVP] wrote:
> On Aug 30, 3:08 am, Arne Vajhøj <> wrote: >> Aamir Mahmood wrote: >>> I have to write a function to shuffle a collection. If anyone has any idea >>> on how to implement it please share with me. >>> The signature of function is: >>> ICollection Shuffle(ICollection c); >> 2.0+ style: > > <snip> > > Shuffling by sorting is going to be (at best) O(n log n). It's fairly > easy to do it as O(n)... admittedly it's more code, but for large > collections it could be significant. True. The advantage of the sorting method is that the methods validity is rather obvious. Arne |
|
|
|
#6 |
|
Guest
Posts: n/a
|
Re: Shuffle a Collection
You can find a nice example here:
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pine Collection List | Dave Miller | Slackware Linux | 8 | 29-08-2007 08:49 PM |
| Web Templates links collection! | sree | Programming | 0 | 14-02-2007 01:07 PM |
| Shuffle Reborn in New Colours | Dark Star | Mobile Phones & Gadgets | 1 | 01-02-2007 01:23 AM |
| Apple fixes iPod Shuffle glitch, again | Dark Star | Technical Discussions | 0 | 01-01-2007 08:08 PM |
< Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |