Shuffle array in Unity

假装没事ソ 提交于 2021-02-05 08:50:08

问题


I want to shuffle the numbers in my array variable once, so I called my Shuffle method from Start().

I then attempt access the shuffled array from update, but I am unable to do so. How can I access it? Is there any other way to shuffle my array once, then use it forever?

private System.Random _random = new System.Random();
float sec;
float timecount;
float starttime;
void Start () {
    starttime = Time.time;
    int[] array = { 1, 2, 3, 4, 5, 6, 7, 8};
    Shuffle(array);
    foreach (int value in array)
    {
        Debug.Log(value);
    }
}

void Update () {
    //time
    timecount = Time.time - starttime;
    sec = (timecount % 60f);
    if (Mathf.Round(sec) == 3f) {
        //access shuffled array
        Debug.Log(array[3]);  <=====error here
    }
}

//for shuffle number from array
void Shuffle(int[] array){
    int p = array.Length;
    for (int n = p-1; n > 0 ; n--)
    {
        int r = _random.Next(1, n);
        int t = array[r];
        array[r] = array[n];
        array[n] = t;
    }
}

回答1:


Currently, your array is declared within your Start() function. This means it can only be accessed from within the Start() function. If you want to be able to access the array from both Start() and Update() you need to increase its scope by declaring it globally. For example:

private System.Random _random = new System.Random();
float sec;
float timecount;
float starttime;

//declare array globally
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8};

void Start () 
{
  starttime = Time.time;
  Shuffle(array);
  foreach (int value in array)
  {
      Debug.Log(value);
  }
}

void Update () 
{
    //now you can access array here, because it is declared globally
}

Additionally, have a look at this MSDN article about scope in C#.




回答2:


Move int[] array = { 1, 2, 3, 4, 5, 6, 7, 8}; outside the Start function.

private System.Random _random = new System.Random();
float sec;
float timecount;
float starttime;

int[] array = { 1, 2, 3, 4, 5, 6, 7, 8 };

void Start()
{
    starttime = Time.time;
    Shuffle(array);
    foreach (int value in array)
    {
        Debug.Log(value);
    }
}

void Update()
{
    //time
    timecount = Time.time - starttime;
    sec = (timecount % 60f);
    if (Mathf.Round(sec) == 3f)
    {
        //access shuffled array
        Debug.Log(array[3]);
    }
}

//for shuffle number from array
void Shuffle(int[] array)
{
    int p = array.Length;
    for (int n = p - 1; n > 0; n--)
    {
        int r = _random.Next(0, n);
        int t = array[r];
        array[r] = array[n];
        array[n] = t;
    }
}

EDIT: Not related to your question.

There is a bug in your Shuffle function. array[0] will always remain 1 and will never change. To fix this, replace int r = _random.Next(1, n); with int r = _random.Next(0, n); in the Shuffle function.




回答3:


There is still a problem with this algorithm. Imagine an array with values 0, 1, 2 The possible shuffles should be:

0 1 2
0 2 1
1 0 2
1 2 0
2 0 1
2 1 0

and the probability of each case should be the same. To achieve this modify int r = _random.Next(0, n+1); Because the max value of Random.Next is exclusive (see doc)



来源:https://stackoverflow.com/questions/36702548/shuffle-array-in-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!