tags:

views:

1139

answers:

7

I have an application that takes the quality results for a manufacturing process and creates graphs both to show Pareto charts of the bad, and also to show production throughput.

To automate the task of testing these statistical procedures I would like to deterministically be able to add records into the database and have the quality tech go to certain graphs and compare to a known good graph. But, I also would like to simulate the results so they would go into the database as if a user was running through the testing process.

One idea I have had is to fill a list with i number good, j number bad1, k number bad 2, etc. And then somehow randomly sort the list before insertion into the database.

So, my question, is there a standard algorithm to take a sorted list of values and create a randomly sorted list?

A: 

Our host has a very good article about card shuffling, I believe some good ideas can be adopted
http://www.codinghorror.com/blog/archives/001008.html

massimogentilini
That was a terrible article, it started out right, Fisher-Yates/Knuth shuffle, then goes horribly wrong by trying to randomly order using guid as a source of randomness. Don't waste your time and just read the Wikipedia article on Fisher-Yates or use a library that implements shuffle correctly.
mjard
A: 

the way i used to do this was to have a loop that ran a number of times that would generate two random numbers between 0 and the length of the list, then swap those two elements.

John Boker
why was this down voted... let me know what im doing wrong.
John Boker
Correct way to do it would be the Fisher-Yates/Knuth shuffle. Check it out on wikipedia.
mjard
A: 

Depends on what you need for "randomness". The easiest way is probably just to through all of the elements into a hash set and iterate over the result. The order you get will be deterministic, but for most intents and purposes can be considered random. Alternatively, you can generate random numbers between [0..length] of the list, picking out elements and pushing them onto a new list. Assuming that list removal is constant time, the result would be a random list generated with O(n) efficiency.

Daniel Spiewak
A: 
Random rnd = new Random();
List<int> orderedList = new List<int>();
List<int> randomList = new List<int>();
while (orderedList.Count != 0)
{
    int index = rnd.Next(0, orderedList.Count);
    randomList.Add(orderedList[index]);
    orderedList.RemoveAt(index);
}
AlbertEin
+11  A: 

You'll want to use a shuffle algorithm. Make sure to use a proper shuffle algorithm and not a home-baked one, because it may introduce some form of subtle pattern to the data. See this post by Jeff Atwood about the problem with using "random enough" shuffles.

Neil Williams
doh! should have thought of the shuffle algorithm. Thanks.
Ray Jenkins
A: 

A simple answer is to have an array or vector, loop once through it, and for each position visited, pick an element at random from the remainder of the list and swap it with the current element.
You need a reasonable random number generator for this.

quamrana
Note that this generates only odd permutations of the original order for an odd-sized list, and only even permutations for an even-sized list. You normally don't care, but if you do care you might care a lot. Fix it by allowing "swap with self". The result is still biassed output, but is surjective.
Steve Jessop
OK, yes I see. So pick at random from the remainder of the list, or itself and perform a swap.
quamrana
+1  A: 

There are some ready implementations available like http://msdn.microsoft.com/en-us/library/bb385047.aspx and http://java.sun.com/javase/6/docs/api/java/util/Collections.html#shuffle(java.util.List)

Kaniu