473,322 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

array random sort

gl
How do I take an array or arraylist, and sort it randomly? Like suppose the
items in it are (1,2,3,4,5) and I want to get it to be in a random order
(2,3,1,4,5). How do you do that? I think it's a call to the array or array
lists sort method, but i'm not exactly sure how you do it.
Nov 17 '05 #1
9 28792

"gl" <gl@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
How do I take an array or arraylist, and sort it randomly? Like suppose
the
items in it are (1,2,3,4,5) and I want to get it to be in a random order
(2,3,1,4,5). How do you do that? I think it's a call to the array or array
lists sort method, but i'm not exactly sure how you do it.

public static List<T> shuffledList(List<T> listToShuffle)
{
/*
* Make a new list of elements picked from listToShuffle
* in a random order.
*/

List<int> ints = new List<int>(listToShuffle.Count); //0, 1,
2, ...
for (int i = 0; i < listToShuffle.Count; i++)
ints.Add(i);

List<T> randList = new List<T>(listToShuffle.Capacity);

for (int k = 0; k < listToShuffle.Count; k++)
{
int randIndx = Common.rand.Next(ints.Count); //random
from 0, 2, .. not already picked
int randK = ints[randIndx];
randList.Add(listToShuffle[randK]);
ints.RemoveAt(randIndx);
}

return randList;
}
Nov 17 '05 #2

"gl" <gl@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
How do I take an array or arraylist, and sort it randomly? Like suppose the
items in it are (1,2,3,4,5) and I want to get it to be in a random order
(2,3,1,4,5). How do you do that? I think it's a call to the array or array
lists sort method, but i'm not exactly sure how you do it.


Sniff....Sniff....Smells like homework.

Search google groups for shuffle algorithm
You should find what you need

Good luck
Bill
Nov 17 '05 #3
I forgot to mention that Common.rand is an instance of Random, and is a
static member of class Common.

You can find this, and some other useful utilities, at
http://www.frontiernet.net/~fredm/dps/Contents.htm

"Fred Mellender" <no****************@frontiernet.net> wrote in message
news:3h*****************@news02.roc.ny...

"gl" <gl@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
How do I take an array or arraylist, and sort it randomly? Like suppose
the
items in it are (1,2,3,4,5) and I want to get it to be in a random order
(2,3,1,4,5). How do you do that? I think it's a call to the array or
array
lists sort method, but i'm not exactly sure how you do it.

public static List<T> shuffledList(List<T> listToShuffle)
{
/*
* Make a new list of elements picked from listToShuffle
* in a random order.
*/

List<int> ints = new List<int>(listToShuffle.Count); //0, 1,
2, ...
for (int i = 0; i < listToShuffle.Count; i++)
ints.Add(i);

List<T> randList = new List<T>(listToShuffle.Capacity);

for (int k = 0; k < listToShuffle.Count; k++)
{
int randIndx = Common.rand.Next(ints.Count); //random
from 0, 2, .. not already picked
int randK = ints[randIndx];
randList.Add(listToShuffle[randK]);
ints.RemoveAt(randIndx);
}

return randList;
}

Nov 17 '05 #4
gl
I'm using c# 1.1 so no generics. :(

"Fred Mellender" wrote:

"gl" <gl@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
How do I take an array or arraylist, and sort it randomly? Like suppose
the
items in it are (1,2,3,4,5) and I want to get it to be in a random order
(2,3,1,4,5). How do you do that? I think it's a call to the array or array
lists sort method, but i'm not exactly sure how you do it.

public static List<T> shuffledList(List<T> listToShuffle)
{
/*
* Make a new list of elements picked from listToShuffle
* in a random order.
*/

List<int> ints = new List<int>(listToShuffle.Count); //0, 1,
2, ...
for (int i = 0; i < listToShuffle.Count; i++)
ints.Add(i);

List<T> randList = new List<T>(listToShuffle.Capacity);

for (int k = 0; k < listToShuffle.Count; k++)
{
int randIndx = Common.rand.Next(ints.Count); //random
from 0, 2, .. not already picked
int randK = ints[randIndx];
randList.Add(listToShuffle[randK]);
ints.RemoveAt(randIndx);
}

return randList;
}

Nov 17 '05 #5
gl
it's definitely not homework. hehe..i wish it was.

I'm basically pulling an array of rows from a dataset, and they have to get
put into a random order (so the same first 3 people don't get a certain value
everytime, it's randomized).

I can check google like you said though.

"Bill Butler" wrote:

"gl" <gl@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
How do I take an array or arraylist, and sort it randomly? Like suppose the
items in it are (1,2,3,4,5) and I want to get it to be in a random order
(2,3,1,4,5). How do you do that? I think it's a call to the array or array
lists sort method, but i'm not exactly sure how you do it.


Sniff....Sniff....Smells like homework.

Search google groups for shuffle algorithm
You should find what you need

Good luck
Bill

Nov 17 '05 #6
"gl" <gl@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
I'm using c# 1.1 so no generics. :(

"Fred Mellender" wrote:


So use ArrayList instead
Bill
Nov 17 '05 #7
Fred Mellender <no****************@frontiernet.net> wrote:

"gl" <gl@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
How do I take an array or arraylist, and sort it randomly? Like suppose
the
items in it are (1,2,3,4,5) and I want to get it to be in a random order
(2,3,1,4,5). How do you do that? I think it's a call to the array or array
lists sort method, but i'm not exactly sure how you do it.


<snip>

Note that it's relatively easy to do this *without* creating two lists
(which Fred's solution does). You can shuffle within the list, by
having an imaginary dividing line between the "randomized" section and
the "unrandomized" section of the list. You start off considering
everything to be unrandomized, then pick a random element to be element
0, swapping it if necessary.

You then take a random element *other than 0*, and swap that into
position 1.

You then take a random element *other than 0 or 1* and swap that into
position 2, etc.

Here's some code which does that with an ArrayList:

public static void Shuffle (ArrayList al)
{
for (int i=0; i < al.Count; i++)
{
object x = al[i];

int index = rng.Next(al.Count-i)+i;
al[i]=al[index];
al[index]=x;
}
}

In each iteration, we're looking to set element i (which is currently
in the "unshuffled" section) to be a random element from the unshuffled
section. At that stage, it becomes part of the shuffled section.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
The problem with Fred's solution is that while it appears to be an O(n)
algorithm, RemoveAt itself is O(n), making the overall algorithm O(n^2). If
the input array get big, it's going to get very slow.

The trick is to avoid deleting anything. We're starting with an array
of N, and we finish with an array of N, so there's no reason to delete
anything. This version is really O(n):

// Shuffles inplace.
public static List<T> shuffleList(List<T> listToShuffle)
{
for (int k = listToShuffle.Count-1; k > 1; --k)
{
int randIndx = Common.rand.Next(k); //
T temp = listToShuffle[k];
listToShuffle[k] = listToShuffle[randIndx]; // move random num to
end of list.
listToShuffle[randIndx] = temp;
}
return listToShuffle;
}

So, say, for example, listToShuffle has 10 elements (0-9). First we pick a
random number between 0 & 8, and we swap the element at the position with
the element at position 9. Then we pick an number between 0 & 7, and swap
that element with [8]. And so on until we're pick a number between 0 & 1 and
swapping it with listToShuffle[2]
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Fred Mellender" <no****************@frontiernet.net> wrote in message
news:3h*****************@news02.roc.ny...

"gl" <gl@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
How do I take an array or arraylist, and sort it randomly? Like suppose
the
items in it are (1,2,3,4,5) and I want to get it to be in a random order
(2,3,1,4,5). How do you do that? I think it's a call to the array or array lists sort method, but i'm not exactly sure how you do it.

public static List<T> shuffledList(List<T> listToShuffle)
{
/*
* Make a new list of elements picked from listToShuffle
* in a random order.
*/

List<int> ints = new List<int>(listToShuffle.Count); //0, 1,
2, ...
for (int i = 0; i < listToShuffle.Count; i++)
ints.Add(i);

List<T> randList = new List<T>(listToShuffle.Capacity);

for (int k = 0; k < listToShuffle.Count; k++)
{
int randIndx = Common.rand.Next(ints.Count); //random
from 0, 2, .. not already picked
int randK = ints[randIndx];
randList.Add(listToShuffle[randK]);
ints.RemoveAt(randIndx);
}

return randList;
}

Nov 18 '05 #9
If it were homework, I suspect he'd be asking about a random shuffle instead
of a random sort. :-)

Pete

"Bill Butler" <qw****@asdf.com> wrote in message
news:2i4ff.49$N44.45@trndny08...

"gl" <gl@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
How do I take an array or arraylist, and sort it randomly? Like suppose
the
items in it are (1,2,3,4,5) and I want to get it to be in a random order
(2,3,1,4,5). How do you do that? I think it's a call to the array or
array
lists sort method, but i'm not exactly sure how you do it.


Sniff....Sniff....Smells like homework.

Search google groups for shuffle algorithm
You should find what you need

Good luck
Bill

Nov 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dr John Stockton | last post by:
Q1 : Given an array such as might have been generated by var A = is there a highly effective way of reducing it to - i.e. removing the undefineds and shifting the rest down? ...
4
by: bg_ie | last post by:
Hi, I have an array as follows - var MultiArray = new Array(2); MultiArray = new Array(num); MultiArray = new Array(num); I've tried randomizing this array using the following but its...
6
by: CodeRazor | last post by:
My array is int list = new int{20,99,6}; Using a for-loop, how can I order the output of this array. I don't want to use a sorted list. I know this is straightforward but can't figure it...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
6
by: Paul van Brenk | last post by:
When you run the Shuffle method often enough it will throw exception. And I can't figure out why. Anybody? Paul van Brenk the code: static void Shuffle(){ int ints = { 1, 2, 3, 4, 5, 6,...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
9
by: Tuxedo | last post by:
I'd like to reorganize the third, fourth, fifth and sixth, as well as any elements thereafter in an array in random order: var a = new...
4
by: drktmplr11 | last post by:
Hi, this is my first post here at the forums, and I am looking for assistance with what looks to be a syntax error within my code. I am attending FIU, and looking to broaden my understanding of...
15
by: DL | last post by:
say, we have the following: // declare an array myArray = ; // short hand? same as myArray = new Array ? // populate it myArray = 0; myArray = 0; myArray = 1; myArray = 0;
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.