Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question loop/array help

Discussion in 'Scripting' started by scarletcode02, May 6, 2022.

  1. scarletcode02

    scarletcode02

    Joined:
    May 6, 2022
    Posts:
    4
    Is it possible to randomly output all elements of an array with a loop?

    like lets say we have a loop and array:


    Code (CSharp):
    1. arrayCount = 10
    2. int array [10] = {1, 2,2, 3,3,3, 4,4,4,4}
    3.  
    4.  
    5.    for (int i = 0; i < arrayCount; i++)
    6.    {
    7.        int x = Random(array);
    8.    }
    obviously my loop code is wrong. But is it possible to output every element randomly (no repeats unless the element appears multiple times i.e. output four 4's but not five times) through a loop function?

    Also I'd like to know if there is an easier way to make an array with repeated elements of varying degrees like the above example or should I just do it manually and write out the array. i.e. how to make an array with one 1, three 2's, three 3's, six 4's, two 5's without just typing out every element. Preferably because I'd like to be able to adjust the frequency each number appears.
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    you would be best to use a List<int> instead of array, then you can shuffle it before doing the loop and get and remove 1 element at a time
     
  3. lambch0p

    lambch0p

    Joined:
    Oct 22, 2014
    Posts:
    62
    Hi,

    What passerbycmc said :)

    I was typing this so will continue posting it.

    With regards to creating the repeating array you could use a 2-dimensional array:
    int[,] array2D = new int[,] { { 1, 1}, { 3, 2 }, { 3, 3 }, { 6, 4 }, { 2, 5 } };

    You can then use the following method to spit out a shuffled list (not single purpose for brevity):


    Code (CSharp):
    1.     private List<int> GetShuffledList(int[,] array2D)
    2.     {
    3.       var tmpList = new List<int>();
    4.       for (int x = 0; x < array2D.GetLength(0); x++)
    5.       {
    6.         var repeat = array2D[x, 0];
    7.         var num = array2D[x, 1];
    8.         for (int y = 0; y < repeat; y++)
    9.         {
    10.           tmpList.Add(num);
    11.         }
    12.       }
    13.       var rnd = new Random();
    14.       var randomized = tmpList.OrderBy(item => rnd.Next()).ToList();
    15.       return randomized;
    16.     }
    Then call it with
    Code (CSharp):
    1. var result = GetShuffledList(array2D);
     
  4. scarletcode02

    scarletcode02

    Joined:
    May 6, 2022
    Posts:
    4
    Thanks this helps so much. I have a follow up question though:

    Code (CSharp):
    1.     private List<int> GetShuffledList(int[,] array2D)
    2.     {
    3.       var tmpList = new List<int>();
    4.       for (int x = 0; x < array2D.GetLength(0); x++)
    5.       {
    6.         var repeat = array2D[x, 0];
    7.         var num = array2D[x, 1];
    8.         for (int y = 0; y < repeat; y++)
    9.         {
    10.           tmpList.Add(num);
    11.         }
    12.       }
    13.      
    14.       return tmpList;
    15.     }
    If I wanted to remove the randomized part like so, would this be the correct way to go about it? Because I'd like GetShuffledList(array2D) to output a list instead of a var.
     
  5. lambch0p

    lambch0p

    Joined:
    Oct 22, 2014
    Posts:
    62
    :) It will output a list. The method signature (private List<int> ....) shows you that it will return a list of type int.

    var is a C# way of implicitly declaring variable types. Basically var will become whatever is returned by the method. It would have been just as valid (and definitely clearer) to have written:

    List<int> result = GetShuffledList(array2D);
     
    scarletcode02 likes this.