none
How Can I Change The Order Of Number In a Array? RRS feed

  • Question

  • I have an int array, its numbers inside of this array are: 
    1,2,3,4,5,6,7,8,9,10

    I want to change the order of this numbers, to do something like this:

    3,2,5,6,7,9,1,10,4,8

    This order should be random and change its value every time it runs. 
    How Can I Do something like this?

    Wednesday, August 22, 2018 8:57 PM

All replies

  • Hello,

     My suggestion is to code random pick of objects from source array.

    Then place random selected objects into another array.  Try to work

    out what code you need that will provide a possible solution.

     

     When you have a basic code project and need further assistance then

    post a new question regarding the issues of that code. Such as syntax

    usage and errors.

     

     I do not provide code solution without seeing OP (Original Poster) efforts.

    Because this is not how a person learns to code programs. You must take

    the time and effort by learning C# language and syntax.  There are many

    books, online tutorials that can provide this aid in the form of instructional

    sample code.

     

     Thanks :)
    Wednesday, August 22, 2018 11:34 PM
  • Hi SebGM2018,

    Thank you for posting here.

    For your question, you could try the code below.

    List<int> array = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                Random rnd = new Random();
                int[] MyArry = array.OrderBy(x => rnd.Next()).ToArray();

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 23, 2018 2:46 AM
    Moderator