How to randomize string list without repeating?

I’m creating quiz game, where questions are randomized, but I don’t know how to do it.
I decided to randomize numbers and use these numbers as index of questions, but it won’t work for me.

using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class Randomization : MonoBehaviour
{
    public Text texxt; // this text is needed to display the question

    public List<string> questions = new List<string>(); 
    public List<int> previousQuestion = new List<int>();

    public int questionNumber = 0;
   
    public static int randQuestion = -1;


    int Lenght = 6;
   public List<int> list = new List<int>();
   public void GenerateRandom()
    {
        for (int j = 0; j < Lenght; j++)
        {
            int Rand = Random.Range(0, 6);
            while (list.Contains(Rand))
            {
                Rand = Random.Range(0, 6);
            }
            list.Add(Rand);
            //Debug.Log(questions[list[j]]);
            print(list[j]);
        }
    }
    void Start()
    {
        GenerateRandom();
        // texxt.text = questions[randQuestion].ToString();
       
       
    }


    void Update()
    {
        //previousQuestion[questionNumber] = randQuestion;
        //questionNumber++;

        //if (randQuestion == -1)
        //{
        //    randQuestion = Random.Range(0, questions.Count);

        //    for (int i = 0; i < 6; i++)
        //    {
        //        if (randQuestion != previousQuestion[i])
        //        {

        //        }

        //        else
        //        {
        //            randQuestion = -1;

        //        }
        //    }
        //}

        //if (randQuestion > -1)
        //{
        //    // texxt.text = questions[randQuestion].ToString();
        //    Debug.Log(questions[randQuestion]);
        //    previousQuestion[questionNumber] = randQuestion;
        //    questionNumber++;
        //}

    }


    //public void RandomCall()
    //{
    //    //for(int i = 0; i < list.Count; i++)
    //    //{
    //     //    Debug.Log(list[0]);//.ToString();
    //    texxt.text=questions[0];
    //    StartCoroutine("Hije");
    //    //}
    //}

    // IEnumerator Hije() //here I try to remove questions, it works nicely
    //{
    //    yield return new WaitForSeconds(1);
    //    list.RemoveAt(0);
    //    questions.RemoveAt(0);
    //}

   

}

Can someone help me, please?

What’s wrong with your solution? It’s not the most performant one, but it looks it has to work.

but random without dublicates works nice with int type in GenerateRandom()
I want random without dublicates with string type

I think that I can use numbers from list as index in questions, but I don’t know how to do this.

Generate the same number of integers as strings you want to select. Then go through the generated list of integers in order from zero up to the maximum number. Use that as index in another collection where you store the strings.
And this way you can have indexed strings without moving around that many strings in memory.

 public List<string> questions = new List<string>() { "a", "b", "c", "d", "e", "f" };

  int indexforq=0;

    int Lenght = 6;
   public List<int> list = new List<int>();
   public void GenerateRandom()
    {
        for (int j = 0; j < Lenght; j++)
        {
            int Rand = Random.Range(0, 6);
            while (list.Contains(Rand))
            {
                Rand = Random.Range(0, 6);
            }
            list.Add(Rand);
            //Debug.Log(questions[list[j]]);
           
            print(list[j]);
        }
       
    }
    void Start()
    {
        GenerateRandom();
        // texxt.text = questions[randQuestion].ToString();

        for (int i = 0; i < list.Count; i++)
        {
            indexforq = list[i];
            //string b = questions.IndexOf(list,0,6);

            Debug.Log(indexforq);
            for (int j = 0; j < 6; j++)
            {
                questions[j] = questions[indexforq];
               

            }

        }

    }

With this code i got this thing

6145238--671060--upload_2020-7-29_12-16-17.png

I don’t know what you’re trying to do exactly, but after the

GenerateRandom();

you need to have one random string in the

var firstRandomQuestion = questions[list[0]];

and every subsequent too, if you want a next random question, just increase the 0 in this code.
You do not need to sort the strings too. Just use them as is.

1 Like

Thank you very much! Finally i know to do it

void Start()
    {
        GenerateRandom();
      
        for (int i = 0; i < list.Count; i++)
        {
            firstRandomQuestion = questions[list[i]];

            Debug.Log(firstRandomQuestion);
        }
1 Like