TechTalkz.com Logo Ask the Expert

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

Notices

Shuffle a Collection

C#(C Sharp)


Reply
 
Thread Tools Display Modes
Old 05-09-2007, 02:30 PM   #1
Aamir Mahmood
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


  Reply With Quote
Old 05-09-2007, 02:30 PM   #2
Arne Vajhøj
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
  Reply With Quote
Old 05-09-2007, 02:30 PM   #3
Aamir Mahmood
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" <aamirmahmood@gmail.com> wrote in message
news:%23xjW$hp6HHA.5984@TK2MSFTNGP04.phx.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
>



  Reply With Quote
Old 05-09-2007, 02:31 PM   #4
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Re: Shuffle a Collection

On Aug 30, 3:08 am, Arne Vajhøj <a...@vajhoej.dk> 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

  Reply With Quote
Old 05-09-2007, 02:39 PM   #5
Arne Vajhøj
Guest
 
Posts: n/a
Re: Shuffle a Collection

Jon Skeet [C# MVP] wrote:
> On Aug 30, 3:08 am, Arne Vajhøj <a...@vajhoej.dk> 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
  Reply With Quote
Old 17-02-2009, 11:15 AM   #6
Yaya
Guest
 
Posts: n/a
Re: Shuffle a Collection

You can find a nice example here:
Yair Levinson's Blog on .NET
  Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pine Collection List Dave Miller Slackware Linux 8 29-08-2007 07:49 PM
Web Templates links collection! sree Programming 0 14-02-2007 12:07 PM
Shuffle Reborn in New Colours Dark Star Mobile Phones & Gadgets 1 01-02-2007 12:23 AM
Apple fixes iPod Shuffle glitch, again Dark Star Technical Discussions 0 01-01-2007 07:08 PM


< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 10:48 PM.


vBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2010, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional