Stop repeat the number on my list?

Hi,
My script give me a list with random numbers but I don’t want it to repeat the numbers. How to do that?

    public int Rand, Lenght = 30, Picker = 1;
    public List<int> Randomlist = new List<int>();
    public bool changeNumber = true;

    void Start()
    {

        Randomlist = new List<int>(new int[Lenght]);

    }

    void Update()
    {

      if (t changeNumber == true)
        {
            for (int j = 1; j < Lenght; j++)
            {
                Rand = Random.Range(0, 30);
                Randomlist[j] = Rand;
                Picker = 1;
                changeNumber = false;
            }

        }
}

Instead of generatimg random numbers, generate first a list of numbers from 0 to 30 ( all of them).

Then use an algorithm like this to randomly shuffle the list:

1 Like

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.

    List<int> xList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
    public List<int> deck = new List<int>();

    void Update()
    {

        if (changeNumber == true)
        {
            foreach (int xInt in xList)
            {
                deck.Insert(Random.Range(0, deck.Count + 1), xInt);
            }
            changeNumber = false;
        }
    }

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 :slight_smile:

https://en.wikipedia.org/wiki/Fisher–Yates_shuffle

1 Like

Thank you Kurt, now my script run well. Here is the new scripts for people like me.

    List<int> xList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
    public List<int> deck = new List<int>();
    void Update()
    {
        if (changeNumber == true)
        {
            deck.Clear();
            foreach (int xInt in xList)
            {
                deck.Insert(Random.Range(0, deck.Count + 1), xInt);
            }
            changeNumber = false;
        }
    }
// take int in, create int-length list starting at offset, randomize it
    public static List<int> createAndRandomizeList(int length, int offset)
    {
        List<int> tempCreateRandList = new List<int>();
        for (int i = 0; i < length; i++)
        {
            tempCreateRandList.Add(i + offset);
        }

        for (int i = 0; i < tempCreateRandList.Count; i++)
        {
            int temp = tempCreateRandList[i];
            int rand = Random.Range(i, tempCreateRandList.Count);
            tempCreateRandList[i] = tempCreateRandList[rand];
            tempCreateRandList[rand] = temp;
        }
        //Debug.Log("tempCreateRandList = " + string.Join(",", tempCreateRandList));
        return tempCreateRandList;
    }
1 Like