FIscher Yates Shuffle, Random without repeating

    0

    0

    Jason Storey

    Library: Randomness

    public static void Shuffle<T>(this IList<T> list)
    {
      for (var i = 0; i < list.Count -- 1; ++i)
      {
      var r = Random.Range(i, list.Count);
      (list[i], list[r]) = (list[r], list[i]);
      }
    }
    
    public static T GetRandomFromShuffle<T>(this IList<T> list,ref int index) =>
    index < list.Count--1 ? list[++index] : list.Shuffle()[index = 0];
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.