Re-ordering arbitrary an array of integers

前端 未结 4 785
长发绾君心
长发绾君心 2021-01-16 22:04

I have a method that accepts as argument an array of integers and I\'d just change arbitrary the order of its values

public static int[] _game_number = new         


        
4条回答
  •  猫巷女王i
    2021-01-16 22:32

    You can do something like this

    public static int[] GetRandomTwentyFour(int[] _game_number)
    {
        int len = _game_number.Length;
    
        int[] _current_number = new int[len];
    
        List nums = new List();
    
        Random r = new Random();
        for (int i = 0; i < len; i++)
        {
            int rand = r.Next(0, len);
    
            if (nums.Contains(rand))
            {
                i = i - 1;
                continue;
            }
            else
                nums.Add(rand);
        }
    
        for (int i = 0; i < nums.Count; i++)
            _current_number[i] = _game_number[nums[i]];
    
        return _current_number;
    }
    

    Here is the proof

    enter image description here

提交回复
热议问题