Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two lists with 4 strings each. I shuffle them and then I select a string on each list to create a combination of strings from both lists. I want the four combinations (written below) repeated four times (so 16 combinations in total) but each of these combination should be presented in a random order.

Dog

Lion

Dog and car

Lion and car

From this code, I shuffle the strings then I select the first string from each list, I iterate using for loops and then generate 16 combinations. This code can shuffle, select and the prints are totally fine BUT prints the first selected string - 4 times, then the second selected string - 4 times. So everything works fine except that the order isn't random. So if dog is selected from the first list, it will print: dog, dog and car, dog, dog and car, then it will do this for lion (for the next 4 prints). So it will always print 4 times the string that gets selected from the first list. Whereas I want those selection to be random. Any advice would be helpful. 


What I have tried:

C#
<pre>using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using System;
using System.Linq;

public class randomLetters : MonoBehaviour
{

    List<string> mylist = new List<string>();
    List<string> mylist2 = new List<string>();


    string selected;
    string selected2;

    void Start()
    {
        mylist.Add("dog");
        mylist.Add("lion");
        mylist.Add("dog");
        mylist.Add("lion");

        mylist2.Add("carfordog");
        mylist2.Add("carforlion");
        mylist2.Add("carfordog");
        mylist2.Add("carforlion");
   
        Shuffle(mylist);
        Shuffle(mylist2);

        StartCoroutine(Wait());
    }


    IEnumerator Wait()
    {


        for (int i = 0; i < 4; i++)
        {
            selected = mylist[i];

            for (int j = 0; j < 4; j++)
            {
                selected2 = mylist2[j];


                    if (selected == "dog" && selected2 == "carfordog")
                    {
                        Debug.Log("Dog and Car");

                    }
                    else if (selected == "dog" && selected2 == "carforlion")
                    {
                        Debug.Log("Dog ");
                    }
                    else if (selected == "lion" && selected2 == "carforlion")
                    {
                        Debug.Log("Lion and Car ");
                    }
                    else if (selected == "lion" && selected2 == "carfordog")
                    {
                        Debug.Log("Lion");
                    }

                    yield return new WaitForSeconds(1);
            }

            yield return new WaitForSeconds(1);



        }

    }
    

    void Shuffle(List<string> lists)
    {
        for (int j = lists.Count - 1; j > 0; j--)
        {
            int rnd = UnityEngine.Random.Range(0, j + 1);
            string temp = lists[j];
            lists[j] = lists[rnd];
            lists[rnd] = temp;
        }
    }

}
Posted
Updated 11-Apr-19 7:41am
Comments
Maciej Los 11-Apr-19 2:02am    
Why Shuffle function loops through the collection of items in a list?
Please, debug the programm to find out what int rnd = UnityEngine.Random.Range(0, j + 1); returns...

1 solution

You should shuffle list2 in the "outer loop" (not before), if you want "combinations within combinations".
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900