W
William Stacey [MVP]
int[] list = {1,2,3,4,5,6};
Function to randomize the list?
Cheers!
Function to randomize the list?
Cheers!
William Stacey said:More specific example/need. Take any ArrayList of length >=2. Now
randomize the elements in most efficient method. TIA
--
William Stacey, MVP
William Stacey said:int[] list = {1,2,3,4,5,6};
Function to randomize the list?
Cheers!
John Wood said:Run the ArrayList.Sort method, passing in a custom comparer that returns -1
or 1 randomly.
?
William Stacey said:More specific example/need. Take any ArrayList of length >=2. Now
randomize the elements in most efficient method. TIA
--
William Stacey, MVP
William Stacey said:int[] list = {1,2,3,4,5,6};
Function to randomize the list?
Cheers!
William Stacey said:That sounds like it should work. Good idea John. Related (kinda) question.
Say you have a collection that contains an arraylist. You export the same
Sort apis on your collection. However each object in the collection is an
internal object that contains the user object, yet you still want the sort
methods to assume sorting on value object, not on Node object. See below.
Thanks again. Cheers!
public class MyList
{
ArrayList al = new ArrayList();
public MyList()
{
}
public Add(object key, object value)
{
Node newNode = Node(key, value);
al.Add(newNode);
}
public void Sort()
{
// Use default Compare of Node.value ??
}
public virtual void Sort(IComparer comparer)
{
// Use comparer on Node.value, Not on Node object itself ??
}
}
public class Node
{
public object key;
public object value;
public Node(object key, object value)
{
this.key = key;
this.value = value;
}
}
--
William Stacey, MVP
John Wood said:Run the ArrayList.Sort method, passing in a custom comparer that returns -1
or 1 randomly.
?
William Stacey said:More specific example/need. Take any ArrayList of length >=2. Now
randomize the elements in most efficient method. TIA
--
William Stacey, MVP
int[] list = {1,2,3,4,5,6};
Function to randomize the list?
Cheers!
John Wood said:Run the ArrayList.Sort method, passing in a custom comparer that returns -1
or 1 randomly.
?
William Stacey said:More specific example/need. Take any ArrayList of length >=2. Now
randomize the elements in most efficient method. TIA
--
William Stacey, MVP
William Stacey said:int[] list = {1,2,3,4,5,6};
Function to randomize the list?
Cheers!
Scott said:
William Stacey said:hmm. After more testing. Sometimes that works, other times you get
Exception about Index outside bounds of array. Must have something to do
with compare when you sending it bogus compare results and thinking there
must be one more element to compare when there is none.
--
William Stacey, MVP
John Wood said:Run the ArrayList.Sort method, passing in a custom comparer that returns -1
or 1 randomly.
?
William Stacey said:More specific example/need. Take any ArrayList of length >=2. Now
randomize the elements in most efficient method. TIA
--
William Stacey, MVP
int[] list = {1,2,3,4,5,6};
Function to randomize the list?
Cheers!
William Stacey said:Here is a version that selects first element randomly, then puts rest in
order (same way Bind does round robin.) I needed another ArrayList for
this, but may be better way. Cheers!
public void RandomizeFirst()
{
Random rand = new Random();
if ( list.Count < 2 )
return;
int randIndex = rand.Next(0, list.Count - 1);
if ( randIndex == 0 )
return;
int index = randIndex;
ArrayList newList = new ArrayList(list.Count);
for(int i = 0; i < list.Count; i++)
{
newList.Add(list[index]);
index = (index + 1) % list.Count;
}
list = newList;
}
--
William Stacey, MVP
Scott said:
Justin Rogers said:This does the same, doesn't require the extra list, and handles a
condition with your and logic. Note if the list is small, this will
work great. Larger lists you may run into some thrasing because
of the removals and insertions. There are better algorithms for
this if you are using array's or linked lists.
private Random rand = new Random();
public void RandomizeFirst() {
if ( list.Count < 2 ) { return; }
// Rand never returns the top number
int randIndex = rand.Next(0, list.Count);
if ( randIndex == 0 ) { return; }
int endList = list.Count - 1;
for(int i = endList; i >= randIndex; i--) {
object item = list[endList];
list.RemoveAt(endList);
list.Insert(0, item);
}
}
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
William Stacey said:Here is a version that selects first element randomly, then puts rest in
order (same way Bind does round robin.) I needed another ArrayList for
this, but may be better way. Cheers!
public void RandomizeFirst()
{
Random rand = new Random();
if ( list.Count < 2 )
return;
int randIndex = rand.Next(0, list.Count - 1);
if ( randIndex == 0 )
return;
int index = randIndex;
ArrayList newList = new ArrayList(list.Count);
for(int i = 0; i < list.Count; i++)
{
newList.Add(list[index]);
index = (index + 1) % list.Count;
}
list = newList;
}
--
William Stacey, MVP
Scott said:Try this: http://www.boyet.com/Articles/SimpleShuffling.html.
Scott
More specific example/need. Take any ArrayList of length >=2. Now
randomize the elements in most efficient method. TIA
William Stacey said:Thanks Justin. Will check this out. Just curious what condition I had with
logic for education purposes. Cheers!
--
William Stacey, MVP
Justin Rogers said:This does the same, doesn't require the extra list, and handles a
condition with your and logic. Note if the list is small, this will
work great. Larger lists you may run into some thrasing because
of the removals and insertions. There are better algorithms for
this if you are using array's or linked lists.
private Random rand = new Random();
public void RandomizeFirst() {
if ( list.Count < 2 ) { return; }
// Rand never returns the top number
int randIndex = rand.Next(0, list.Count);
if ( randIndex == 0 ) { return; }
int endList = list.Count - 1;
for(int i = endList; i >= randIndex; i--) {
object item = list[endList];
list.RemoveAt(endList);
list.Insert(0, item);
}
}
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
William Stacey said:Here is a version that selects first element randomly, then puts rest in
order (same way Bind does round robin.) I needed another ArrayList for
this, but may be better way. Cheers!
public void RandomizeFirst()
{
Random rand = new Random();
if ( list.Count < 2 )
return;
int randIndex = rand.Next(0, list.Count - 1);
if ( randIndex == 0 )
return;
int index = randIndex;
ArrayList newList = new ArrayList(list.Count);
for(int i = 0; i < list.Count; i++)
{
newList.Add(list[index]);
index = (index + 1) % list.Count;
}
list = newList;
}
--
William Stacey, MVP
Try this: http://www.boyet.com/Articles/SimpleShuffling.html.
Scott
More specific example/need. Take any ArrayList of length >=2. Now
randomize the elements in most efficient method. TIA
John Wood said:Run the ArrayList.Sort method, passing in a custom comparer that returns -1
or 1 randomly.
?
John Wood said:Run the ArrayList.Sort method, passing in a custom comparer that returns -1
or 1 randomly.
?
William Stacey said:More specific example/need. Take any ArrayList of length >=2. Now
randomize the elements in most efficient method. TIA
--
William Stacey, MVP
William Stacey said:int[] list = {1,2,3,4,5,6};
Function to randomize the list?
Cheers!
William Stacey said:int[] list = {1,2,3,4,5,6};
Function to randomize the list?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.