- Home /
C# Randomly Adding Elements from stringListA to stringListB
I'm trying to take a random amount of elements from stringListA and add it to stringListB. I also want to randomize the order of elements that are added to stringListB. I was able to randomize the order but I'm not sure how to randomize the amount.
 public class RandomAmountAndOrder : MonoBehaviour {
   public List <string> stringListA;
   public List <string> stringListB;
   void Start(){
     stringListA = new <string>(){
      "one",
      "two",
      "three",
      "four",
      "five",
     };
     for (int a = 0; a < text.Length; a++){
       stringListB = new <string>(){
        stringListA[UnityEngine.Random.Range(0, 5)],
        stringListA[UnityEngine.Random.Range(0, 5)],
        stringListA[UnityEngine.Random.Range(0, 5)],
        stringListA[UnityEngine.Random.Range(0, 5)],
        stringListA[UnityEngine.Random.Range(0, 5)],
       };
     }
   }
 }
               Comment
              
 
               
              I'm assu$$anonymous$$g adding the same value more than once is deliberate?
 
               Best Answer 
              
 
              Answer by clunk47 · Feb 11, 2014 at 04:25 AM
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Example : MonoBehaviour 
 {
     List<string> a = new List<string>(){"1", "2", "3", "4", "5"};
     List<string> b = new List<string>();
     
     IEnumerator Start()
     {
         while(true)
         {
             for(int i = Random.Range (0, a.Count); i < a.Count; i++)
             {
                 b.Add (a[Random.Range (0, a.Count)]);
             }
         
             foreach(string s in b)
                 print(s);
                 
             yield return new WaitForSeconds(1);
             //Reset b
             b = new List<string>();
         }
     }
 }
 
Your answer
 
 
             Follow this Question
Related Questions
A node in a childnode? 1 Answer
C# Adding Multiple Elements to a List on One Line 5 Answers
C# Dividing Gameobject List by Half 1 Answer
C# Displaying a List in Series 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                