Egox
0
Q:

c# random choice

list[Random.Range(0, list.Count)];
4
using System;
using System.Collections.Generic;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         var random = new Random();
         var list = new List<string>{ "one","two","three","four"};
         int index = random.Next(list.Count);
         Console.WriteLine(list[index]);
      }
   }
}
0
/// <summary>
/// Get random values from a list and return a list of chosen items
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="passedList"></param>
/// <param name="numberToChoose"></param>
/// <returns></returns>
public List<T> GetRandomFromList<T>(List<T> passedList, int numberToChoose)
{
    System.Random rnd = new System.Random();
    List<T> chosenItems = new List<T>();

    for (int i = 1; i <= numberToChoose; i++)
    {
      int index = rnd.Next(passedList.Count);
      chosenItems.Add(passedList[index]);
    }

    //Debug.Log(chosenItems.Count);

    return chosenItems;
}
-1

New to Communities?

Join the community