364,085 Members | 5185 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Randomize array items

Fieldmedic
P: n/a
Fieldmedic
I'm trying to determine the best way to randomize a set of array items. I'm
thinking that I could use an arraylist and have it use the lower and upper
bounds as the limits. Any suggestions?

Cheers,
Steve
Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


Jon Skeet [C# MVP]
P: n/a
Jon Skeet [C# MVP]
Fieldmedic <Fieldmedic@discussions.microsoft.com> wrote:[color=blue]
> I'm trying to determine the best way to randomize a set of array items. I'm
> thinking that I could use an arraylist and have it use the lower and upper
> bounds as the limits. Any suggestions?[/color]

There's no need to use an ArrayList - the size doesn't need to change.

Here's a method which will actually shuffle *any* IList:

static Random rng = new Random();
static void Randomize(IList list)
{

for (int i=list.Count-1; i > 0; i--)

{
int swapIndex = rng.Next(i+1);
if (swapIndex != i)
{
object tmp = list[swapIndex];
list[swapIndex] = list[i];
list[i] = tmp;
}
}
}

The way it works is to mentally divide the list into two halves - the
unshuffled half (in the range [0,i]) and the shuffled half (in the
range [i+1, list.Count-1]).

In each iteration, it picks a random element from the unshuffled half,
and swaps it with the element at the end of the unshuffled half, then
moves the boundary down.

Note that if you're shuffling arrays of value types, you should use a
strongly typed version of the above, changing the declaration of both
list and tmp appropriately, to avoid boxing.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

Fieldmedic
P: n/a
Fieldmedic
Jon,
Thanks for the input! The reason I was thinking arraylist was because
the "list" of items will actually come from the selected items in a ListBox.
The ListBox connects to a database and the user can select from the items in
it to narrow create the list of items to be randomized. I'll give your code a
shot, although I have to convert it to VB. :)

Thanks again,
Steve


"Jon Skeet [C# MVP]" wrote:
[color=blue]
> Fieldmedic <Fieldmedic@discussions.microsoft.com> wrote:[color=green]
> > I'm trying to determine the best way to randomize a set of array items. I'm
> > thinking that I could use an arraylist and have it use the lower and upper
> > bounds as the limits. Any suggestions?[/color]
>
> There's no need to use an ArrayList - the size doesn't need to change.
>
> Here's a method which will actually shuffle *any* IList:
>
> static Random rng = new Random();
> static void Randomize(IList list)
> {
>
> for (int i=list.Count-1; i > 0; i--)
>
> {
> int swapIndex = rng.Next(i+1);
> if (swapIndex != i)
> {
> object tmp = list[swapIndex];
> list[swapIndex] = list[i];
> list[i] = tmp;
> }
> }
> }
>
> The way it works is to mentally divide the list into two halves - the
> unshuffled half (in the range [0,i]) and the shuffled half (in the
> range [i+1, list.Count-1]).
>
> In each iteration, it picks a random element from the unshuffled half,
> and swaps it with the element at the end of the unshuffled half, then
> moves the boundary down.
>
> Note that if you're shuffling arrays of value types, you should use a
> strongly typed version of the above, changing the declaration of both
> list and tmp appropriately, to avoid boxing.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>[/color]
Jul 21 '05 #3

Post your reply

Help answer this question



Didn't find the answer to your .NET Framework question?

You can also browse similar questions: .NET Framework