Search Unity

Resolved Stop repeat the number on my list?

Discussion in 'Scripting' started by Quast, Mar 20, 2021.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Hi,
    My script give me a list with random numbers but I don't want it to repeat the numbers. How to do that?

    Code (CSharp):
    1.  
    2.     public int Rand, Lenght = 30, Picker = 1;
    3.     public List<int> Randomlist = new List<int>();
    4.     public bool changeNumber = true;
    5.  
    6.     void Start()
    7.     {
    8.  
    9.         Randomlist = new List<int>(new int[Lenght]);
    10.  
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.  
    16.       if (t changeNumber == true)
    17.         {
    18.             for (int j = 1; j < Lenght; j++)
    19.             {
    20.                 Rand = Random.Range(0, 30);
    21.                 Randomlist[j] = Rand;
    22.                 Picker = 1;
    23.                 changeNumber = false;
    24.             }
    25.  
    26.         }
    27. }
    28.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Quast likes this.
  3. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Thank you for your help. I read some answers there and not all fit my simple script. Some freeze the engine !!
    One of them was good but the problem is when I use bool to regenerate the numbers I double the values !! If you could solve this will be good.
    Code (CSharp):
    1.     List<int> xList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
    2.     public List<int> deck = new List<int>();
    3.  
    4.     void Update()
    5.     {
    6.  
    7.         if (changeNumber == true)
    8.         {
    9.             foreach (int xInt in xList)
    10.             {
    11.                 deck.Insert(Random.Range(0, deck.Count + 1), xInt);
    12.             }
    13.             changeNumber = false;
    14.         }
    15.     }
    16.  
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Any shuffle routine with a bug in it that does not exit will freeze the engine. This is by design.

    Clear the
    deck
    before you go stuffing random values into it.

    https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.clear?view=net-5.0

    More performant and with FAR less memory churn, just use a basic Fisher-Yates shuffle. But make it properly as the Wikipedia pseudocode shows, without bugs :)

    https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
     
    Quast likes this.
  5. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Thank you Kurt, now my script run well. Here is the new scripts for people like me.
    Code (CSharp):
    1.     List<int> xList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
    2.     public List<int> deck = new List<int>();
    3.     void Update()
    4.     {
    5.         if (changeNumber == true)
    6.         {
    7.             deck.Clear();
    8.             foreach (int xInt in xList)
    9.             {
    10.                 deck.Insert(Random.Range(0, deck.Count + 1), xInt);
    11.             }
    12.             changeNumber = false;
    13.         }
    14.     }
     
  6. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    Code (CSharp):
    1. // take int in, create int-length list starting at offset, randomize it
    2.     public static List<int> createAndRandomizeList(int length, int offset)
    3.     {
    4.         List<int> tempCreateRandList = new List<int>();
    5.         for (int i = 0; i < length; i++)
    6.         {
    7.             tempCreateRandList.Add(i + offset);
    8.         }
    9.  
    10.         for (int i = 0; i < tempCreateRandList.Count; i++)
    11.         {
    12.             int temp = tempCreateRandList[i];
    13.             int rand = Random.Range(i, tempCreateRandList.Count);
    14.             tempCreateRandList[i] = tempCreateRandList[rand];
    15.             tempCreateRandList[rand] = temp;
    16.         }
    17.         //Debug.Log("tempCreateRandList = " + string.Join(",", tempCreateRandList));
    18.         return tempCreateRandList;
    19.     }
     
    Quast likes this.