469,283 Members | 2,349 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,283 developers. It's quick & easy.

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
Aug 30 '07 #1
4 7410
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" <aa**********@gmail.comwrote in message
news:%2****************@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

Aug 30 '07 #2
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<TShuffle(ICollection<Tc)
{
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
Aug 30 '07 #3
On Aug 30, 3:08 am, Arne Vajhøj <a...@vajhoej.dkwrote:
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

Aug 30 '07 #4
Jon Skeet [C# MVP] wrote:
On Aug 30, 3:08 am, Arne Vajhøj <a...@vajhoej.dkwrote:
>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
Sep 1 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by JC | last post: by
24 posts views Thread by Joerg Schuster | last post: by
3 posts views Thread by songie D | last post: by
25 posts views Thread by tooru honda | last post: by
84 posts views Thread by jacob navia | last post: by
reply views Thread by =?ISO-8859-1?Q?Arne_Vajh=F8j?= | last post: by
1 post views Thread by anjkris07 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.