By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,286 Members | 2,422 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,286 IT Pros & Developers. It's quick & easy.

Randomize array items

P: n/a
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


P: n/a
Fieldmedic <Fi********@discussions.microsoft.com> wrote:
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?


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

P: n/a
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:
Fieldmedic <Fi********@discussions.microsoft.com> wrote:
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?


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.