0

This program suppose to generate 4 random numbers for each 4 arrays from 1 to 10 and it does, but each 4 labels have same values. for example "Usercard"={8,4,2,9} and there is no different numbers in other 3 i don't get it why?!!

     public Form1()
    {
        InitializeComponent();
        Label[] UserCard = { UC1, UC2, UC3, UC4 };
        Label[] P2Card = { P2C1,P2C2, P2C3, P2C4 };
        Label[] P3Card = { P3C1, P3C2, P3C3, P3C4 };
        Label[] P4Card = { P4C1, P4C2,P4C3,P4C4 };
        CardShuffle(UserCard);CardShuffle(P2Card);CardShuffle(P3Card);CardShuffle(P4Card);
    }
    private Array CardShuffle(Label[] Labels)
    {
        Random R = new Random();
        int Counter = 9;
        int[] Numbers = {1,2,3,4,5,6,7,8,9,10};
        int ArrayElement;
        for (int i = 0; i < Labels.Length; i++)
        {
            ArrayElement = R.Next(0, Counter);
            Labels[i].Text = Numbers[ArrayElement].ToString();
            for (int j = ArrayElement; j < Numbers.Length - 1; j++)
            {
                Numbers[j] = Numbers[j + 1];
            }
            Counter--;                
        }
        return Labels;
    }
  • I would really recommend following the common [coding style](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions). That would make the example code less confusing to read. I would also suggest a better description of what it is you are trying to do. It sounds like you want a shuffle, but it does not look correct to me. See [randomizing a list](https://stackoverflow.com/questions/273313/randomize-a-listt) – JonasH May 04 '22 at 08:11
  • There are likely several issues with your shuffle, but the most pressing is that you are repeatedly very quickly creating a `Random` in a loop, which will result it in having the same (clock-based) seed and thus generating the same sequence of numbers. Create it once in your `Form()` constructor and pass it to `CardShuffle()` to avoid that issue. – Matthew Watson May 04 '22 at 08:15
  • Also see here for a correct shuffle implementation: https://stackoverflow.com/questions/273313/randomize-a-listt/1262619#1262619 - but watch out for the highly-upvoted BUT INCORRECT answers to that question! The response marked as the actual answer is correct, however. – Matthew Watson May 04 '22 at 08:17
  • let me explain it better.i do exactly want a shuffle that from 1 to 10 pick a number and put it in my deck.each person has a four card deck that includes a randomly card with values(1,10) but after i run the program all four person have the exact same deck if user has {2,3,4,5} the other ones have the same – Reza Rezaee May 04 '22 at 08:21
  • Yes, but see the duplicate and read my comments. You're using `Random` incorrectly. – Matthew Watson May 04 '22 at 08:26
  • i appreciate a lot that problem solved,i read your comments actually i'm new in this i saw those links u sent me but they are too hard to understand, hope in the future i get it. again thank u. – Reza Rezaee May 04 '22 at 16:13

0 Answers0