Click here to Skip to main content
15,887,464 members
Home / Discussions / C#
   

C#

 
AnswerRe: Problem with shuffling array elements and displaying them Pin
Eddy Vluggen16-Jan-14 3:03
professionalEddy Vluggen16-Jan-14 3:03 
GeneralRe: Problem with shuffling array elements and displaying them Pin
Member 1052812317-Jan-14 3:53
Member 1052812317-Jan-14 3:53 
AnswerRe: Problem with shuffling array elements and displaying them Pin
BillWoodruff16-Jan-14 4:03
professionalBillWoodruff16-Jan-14 4:03 
GeneralRe: Problem with shuffling array elements and displaying them Pin
CPallini16-Jan-14 21:43
mveCPallini16-Jan-14 21:43 
AnswerRe: Problem with shuffling array elements and displaying them Pin
OriginalGriff16-Jan-14 8:31
mveOriginalGriff16-Jan-14 8:31 
GeneralRe: Problem with shuffling array elements and displaying them Pin
BillWoodruff16-Jan-14 17:33
professionalBillWoodruff16-Jan-14 17:33 
GeneralRe: Problem with shuffling array elements and displaying them Pin
OriginalGriff16-Jan-14 19:51
mveOriginalGriff16-Jan-14 19:51 
AnswerRe: Problem with shuffling array elements and displaying them Pin
BillWoodruff16-Jan-14 19:41
professionalBillWoodruff16-Jan-14 19:41 
Two more candidates for the beauty contest:

Candidate One:
C#
private Random newRandom = new Random();

private List<int> MakeShuffledList1(int nItems)
{
    return Enumerable.Range(1, nItems).ToList().OrderBy(item => newRandom.Next()).ToList();
}

// test
List<int> newList = MakeShuffledList1(10);



Candidate Two:
C#
// if thread safety is not an issue: make static ?
private Random newRandom = new Random();

private List<int> MakeShuffledList2(int nItems)
{
    List<int> shuffleList = Enumerable.Range(1, nItems).ToList();
    
    List<int> resultList = new List<int>();
    
    int currentValue;
    
    while (shuffleList.Count > 0)
    {
        currentValue = shuffleList[newRandom.Next(0, shuffleList.Count - 1)];
        resultList.Add(currentValue);
        shuffleList.Remove(currentValue);
    }
    
    return resultList;
}
This follows the usual beauty pageant drill: when Miss/Mr. Whatever is crowned, you know he/she will not be in the next selection category.

Test:
C#
List<List<int>> bunchOLists = new List<List<int>>();

for (int i = 0; i < 10; i++)
{
    bunchOLists.Add(MakeShuffledList1(5));
    bunchOLists.Add(MakeShuffledList2(5));
}

// test

bool IsDuplicate = false;

List<int> currentList;

for (int i = 0; i < bunchOLists.Count - 1; i++)
{
    currentList = bunchOLists[i];

    for (int j = 0; j < bunchOLists.Count - 1; j++)
    {
        if(i != j)
        {
            IsDuplicate = currentList.SequenceEqual(bunchOLists[j]);
        }
    }
}

Console.WriteLine("all List<int> are unique: " + (! IsDuplicate).ToString());

“But I don't want to go among mad people,” Alice remarked.

“Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.”

“How do you know I'm mad?” said Alice.

“You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll


modified 17-Jan-14 2:13am.

AnswerMessage Closed Pin
16-Jan-14 22:43
harold aptroot16-Jan-14 22:43 
GeneralRe: All of the above is actually wrong Pin
CPallini16-Jan-14 23:38
mveCPallini16-Jan-14 23:38 
QuestionDIfference between Q/A and Discussion? Pin
agent_kruger16-Jan-14 1:43
professionalagent_kruger16-Jan-14 1:43 
AnswerRe: DIfference between Q/A and Discussion? Pin
Richard MacCutchan16-Jan-14 2:10
mveRichard MacCutchan16-Jan-14 2:10 
AnswerRe: DIfference between Q/A and Discussion? PinPopular
BillWoodruff16-Jan-14 6:02
professionalBillWoodruff16-Jan-14 6:02 
GeneralRe: DIfference between Q/A and Discussion? Pin
agent_kruger16-Jan-14 23:38
professionalagent_kruger16-Jan-14 23:38 
GeneralRe: DIfference between Q/A and Discussion? Pin
Pete O'Hanlon16-Jan-14 23:55
mvePete O'Hanlon16-Jan-14 23:55 
QuestionHow to use SQL store procedure in LINQ in C#? Pin
Brijesh Kumar Prajapati16-Jan-14 0:09
Brijesh Kumar Prajapati16-Jan-14 0:09 
AnswerRe: How to use SQL store procedure in LINQ in C#? Pin
Eddy Vluggen16-Jan-14 0:17
professionalEddy Vluggen16-Jan-14 0:17 
QuestionApplication requires Microsoft.Vbe.Interop Ver .... installed in GAC Pin
emma.sun.sts15-Jan-14 22:35
emma.sun.sts15-Jan-14 22:35 
AnswerRe: Application requires Microsoft.Vbe.Interop Ver .... installed in GAC Pin
Wayne Gaylard15-Jan-14 23:05
professionalWayne Gaylard15-Jan-14 23:05 
GeneralRe: Application requires Microsoft.Vbe.Interop Ver .... installed in GAC Pin
emma.sun.sts16-Jan-14 23:10
emma.sun.sts16-Jan-14 23:10 
AnswerRe: Application requires Microsoft.Vbe.Interop Ver .... installed in GAC Pin
Freak3016-Jan-14 2:17
Freak3016-Jan-14 2:17 
GeneralRe: Application requires Microsoft.Vbe.Interop Ver .... installed in GAC Pin
emma.sun.sts16-Jan-14 23:05
emma.sun.sts16-Jan-14 23:05 
AnswerRe: Application requires Microsoft.Vbe.Interop Ver .... installed in GAC Pin
jschell16-Jan-14 8:33
jschell16-Jan-14 8:33 
GeneralRe: Application requires Microsoft.Vbe.Interop Ver .... installed in GAC Pin
emma.sun.sts16-Jan-14 23:03
emma.sun.sts16-Jan-14 23:03 
GeneralRe: Application requires Microsoft.Vbe.Interop Ver .... installed in GAC Pin
jschell17-Jan-14 12:59
jschell17-Jan-14 12:59 

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.