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

C#

 
AnswerRe: C# tamir sharp ssh, scp incomplete file transfer Pin
Eddy Vluggen18-Jan-14 0:58
professionalEddy Vluggen18-Jan-14 0:58 
GeneralRe: C# tamir sharp ssh, scp incomplete file transfer Pin
Michael Gaida27-Jan-14 2:23
Michael Gaida27-Jan-14 2:23 
QuestionProblem with shuffling array elements and displaying them Pin
Member 1052812316-Jan-14 2:36
Member 1052812316-Jan-14 2:36 
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 
Just to add to what Bill and Eddy say, you should not declare the Random instance within the Shuffle method: it is initialized from the system clock when it is created, so if you call Shuffle twice in quick sucession, you could get the same random sequence.
Instead, declare it as a private class level variable and reuse the same instance each time Shuffle is called:
C#
private static Random rndNumber = new Random();
public static void Shuffle(int[] a)
{
    int n = a.Length;
    ...


There is another detail: did you notice that the first element is never shuffled? That's because your Random.Next call can't return zero...

BTW: Length is a property of an array - you don't need to treat it as a method! (And the compiler will complain if you do)

If you don't mind my saying, that isn't quite the way I'd do it. I don't know how much you know yet, but I wouldn't do an in-place shuffle. I'd would arrange that the Shuffle method returned a new set of values rather than reorganising the original, and I'd probably make it generic. Heck, does this make any sense to you?
C#
    int[] a = new int[arraySize];
    //Fill the array with unique values
    for (int i = 0; i < a.Length; i++)
        {
        a[i] = i + 1;
        }
    int[] b = Shuffle(a).ToArray();
    Console.WriteLine("\nThe array will now be displayed\n");
    foreach (int i in b)
        {
        Console.WriteLine(i);
        }
    }
private static Random rndNumber = new Random();
public static IEnumerable<T> Shuffle<T>(IEnumerable<T> a)
    {
    int values = a.Count();
    List<T> output = new List<T>(values);
    List<T> input = a.ToList();
    for (int index = 0; index < values; index++)
        {
        int i = rndNumber.Next(0, input.Count());
        output.Add(input[i]);
        input.RemoveAt(i);
        }
    return output;
    }

Never underestimate the power of stupid things in large numbers
--- Serious Sam

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 
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 

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.