U_在数组中不放回地取两个值

using UnityEngine;

public class ArrayPicker : MonoBehaviour
{
    private int[] array = { 1, 2, 3, 4, 5 };
    private int index1;
    private int index2;

    private void Start()
    {
        PickTwoValues();
        Debug.Log("Index 1: " + index1);
        Debug.Log("Index 2: " + index2);
    }

    private void PickTwoValues()
    {
        // 随机选择第一个索引
        index1 = Random.Range(0, array.Length);

        // 在剩余的索引中随机选择第二个索引
        int remainingLength = array.Length - 1;
        int randomOffset = Random.Range(0, remainingLength);
        index2 = (index1 + randomOffset + 1) % array.Length;
    }
}

posted @ 2024-01-09 16:09  匿鱼  阅读(37)  评论(0)    收藏  举报