Click here to Skip to main content
15,391,695 members
Home / Discussions / C#
   

C#

 
AnswerRe: Random Permutation Interface Pin
BillWoodruff12-Sep-12 0:05
mveBillWoodruff12-Sep-12 0:05 
AnswerRe: Random Permutation Interface Pin
Pete O'Hanlon12-Sep-12 0:18
mvaPete O'Hanlon12-Sep-12 0:18 
GeneralRe: Random Permutation Interface Pin
Dave Kreskowiak12-Sep-12 2:21
mveDave Kreskowiak12-Sep-12 2:21 
AnswerRe: Random Permutation Interface Pin
BobJanova12-Sep-12 1:17
MemberBobJanova12-Sep-12 1:17 
GeneralRe: Random Permutation Interface Pin
Pete O'Hanlon12-Sep-12 1:27
mvaPete O'Hanlon12-Sep-12 1:27 
GeneralRe: Random Permutation Interface Pin
Skippums12-Sep-12 6:41
MemberSkippums12-Sep-12 6:41 
AnswerRe: Random Permutation Interface Pin
PIEBALDconsult12-Sep-12 3:07
professionalPIEBALDconsult12-Sep-12 3:07 
AnswerRe: Random Permutation Interface Pin
Skippums12-Sep-12 7:32
MemberSkippums12-Sep-12 7:32 
Thanks everyone for the feedback! The solution I went with is the following:

C#
public static class Permutation {

    // Random number generator
    private static Random rand = new Random();

    // Permute a collection of objects, returning an array
    public static T[] Permute<T>(IEnumerable<T> list) {
        // Create a copy of the input array
        T[] permArray = list.ToArray();
        long arraySize = permArray.LongLength;
 
        // Create a permutation of indexes
        long[] perm = Permutation.Random(arraySize);

        // Assign the new values to the existing ordered collection
        long i = 0;
        foreach (T item in list) {
            permArray[perm[i++]] = item;
        }

        // Return the permuted array
        return permArray;
    }

    // Generates a random permutation of the integers in the range [0, size)
    public static long[] Random(long size) {
        long[] perm = new long[size];
        lock(rand) {
            for (long i = size - 2; i >= 0; --i) {
                // I implemented the extension method Random.NextInt64
                perm[i] = rand.NextInt64(size - i);
                for (long j = i + 1; j < size; ++j) {
                    if (perm[i] <= perm[j])
                        ++perm[j];
                }
            }
        }
        return perm;
    }
}
This will allow a client a way to get a permutation from a set or dictionary as well. Thanks again for the ideas!
Sounds like somebody's got a case of the Mondays

-Jeff


modified 13-Sep-12 13:32pm.

GeneralRe: Random Permutation Interface Pin
BillWoodruff13-Sep-12 2:37
mveBillWoodruff13-Sep-12 2:37 
GeneralRe: Random Permutation Interface Pin
PIEBALDconsult13-Sep-12 3:23
professionalPIEBALDconsult13-Sep-12 3:23 
GeneralRe: Random Permutation Interface Pin
PIEBALDconsult13-Sep-12 3:27
professionalPIEBALDconsult13-Sep-12 3:27 
GeneralRe: Random Permutation Interface Pin
Skippums13-Sep-12 7:27
MemberSkippums13-Sep-12 7:27 
Questionconvert xdocument to integer Pin
rachel_m11-Sep-12 12:16
Memberrachel_m11-Sep-12 12:16 
AnswerRe: convert xdocument to integer Pin
Pete O'Hanlon11-Sep-12 12:22
mvaPete O'Hanlon11-Sep-12 12:22 
GeneralRe: convert xdocument to integer Pin
rachel_m11-Sep-12 16:04
Memberrachel_m11-Sep-12 16:04 
GeneralRe: convert xdocument to integer Pin
Eddy Vluggen11-Sep-12 20:55
professionalEddy Vluggen11-Sep-12 20:55 
GeneralRe: convert xdocument to integer Pin
rachel_m12-Sep-12 4:44
Memberrachel_m12-Sep-12 4:44 
GeneralRe: convert xdocument to integer Pin
PIEBALDconsult13-Sep-12 3:29
professionalPIEBALDconsult13-Sep-12 3:29 
GeneralRe: convert xdocument to integer Pin
Pete O'Hanlon11-Sep-12 22:16
mvaPete O'Hanlon11-Sep-12 22:16 
QuestionCOM+ Event System Pin
Member 813710511-Sep-12 11:40
MemberMember 813710511-Sep-12 11:40 
AnswerRe: COM+ Event System Pin
Bernhard Hiller11-Sep-12 20:24
MemberBernhard Hiller11-Sep-12 20:24 
Question.Net Validation Pin
rmorton11-Sep-12 8:45
Memberrmorton11-Sep-12 8:45 
Answeroracle exception Pin
yaswanth00111-Sep-12 7:19
Memberyaswanth00111-Sep-12 7:19 
AnswerRe: oracle exception Pin
Andrei Straut11-Sep-12 7:28
MemberAndrei Straut11-Sep-12 7:28 
AnswerRe: oracle exception Pin
Abhinav S11-Sep-12 7:56
MemberAbhinav S11-Sep-12 7:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.