473,382 Members | 1,386 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,382 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 28804

"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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.