471,604 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,604 software developers and data experts.

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 7509
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 XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.