Methods call in random order (C#) -
i want write c# program executes several methods a(), b() , c() in random order. how can that?
assuming random number generator declared this:
public static random rnd = new random();
let’s define shuffle
function bring list random order:
/// <summary> /// brings elements of given list random order /// </summary> /// <typeparam name="t">type of elements in list.</typeparam> /// <param name="list">list shuffle.</param> /// <returns>the list operated on.</returns> public static ilist<t> shuffle<t>(this ilist<t> list) { if (list == null) throw new argumentnullexception("list"); (int j = list.count; j >= 1; j--) { int item = rnd.next(0, j); if (item < j - 1) { var t = list[item]; list[item] = list[j - 1]; list[j - 1] = t; } } return list; }
this shuffle implementation courtesy of romkyns!
now put methods in list, shuffle, run them:
var list = new list<action> { a, b, c }; list.shuffle(); list.foreach(method => method());
Comments
Post a Comment