Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Give items unique random numbers

Discussion in 'Scripting' started by Chrisbii, Nov 27, 2014.

  1. Chrisbii

    Chrisbii

    Joined:
    Oct 28, 2014
    Posts:
    9
    Is there some easy way to give a list of items random numbers, but not the same number twice?

    In the Random.Range can we only set min and max value but not exceptions

    Just to be clear

    Code (CSharp):
    1. for (int i = 0; i < 3; i++) {
    2.     item[i] = Random.Range(0, 3);
    3. }
    Like this but with unique numbers on every item.
     
  2. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    Maybe something like this:
    Code (CSharp):
    1. float newValue = 0;
    2. int itemCount = 0
    3.  
    4. while (itemCount < 3)
    5. {
    6.    newValue = Random.Range(0, 3);
    7.    if (Array.IndexOf(item, newValue) < 0)
    8.    {
    9.       item[itemCount] = newValue;
    10.       itemCount++;    
    11.    }
    12. }
    13.  
     
    Last edited: Nov 27, 2014
    rakkarage likes this.
  3. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    682
    Code (CSharp):
    1. System.Guid myGUID = System.Guid.NewGuid();
     
    Senshi, AlucardJay and mweldon like this.
  4. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,137
    You can make a list of numbers in a pool, then assign and remove them at random. You'll need import System.collections.generic; to use a list. This will work, if you have a pool of numbers in mind that you want to use.

    Code (CSharp):
    1. List<int> pool = new List<int>();
    2. for(int i = 0;i < 30;i++)
    3. {
    4.     pool.add(i);
    5. }
    6.  
    7. for(int i = 0;i < item.Length;i++)
    8. {
    9.     item[i] = pool[Random.Range(0, pool.Count)];
    10.     pool.RemoveAt(i);
    11. }
     
    AlucardJay likes this.
  5. mweldon

    mweldon

    Joined:
    Apr 19, 2010
    Posts:
    109
    This is the answer.
     
  6. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    319
    yes, it is (after reading the msdn, I learned something! thank you)
    Tomnnns example is a great gateway to pools, shuffling, etc
     
  7. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    Just a short note about Tomnnns code:
    If you delete items from a list I would recommend to do this from the upper end to the lower end otherwise it's possible to run into an AV (as soon as i is larger than the rest size of the list).

    Code (CSharp):
    1. for(int i = item.Length;i > 0;i--)
    2. {
    3.     item[item.Length - (item.Length - i)] = pool[Random.Range(0, pool.Count)];
    4.     pool.RemoveAt(i);
    5. }
    Beside of this it just reduces the likeliness for duplicate values, but does not avoid it for sure.
     
  8. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,137
    A few honorable mentions, woo :D

    I'm glad nobody mentioned the slow, sometimes incomplete method of checking for duplicated and trying to re-randomize the duplicate indices.