public void DisTaskTest() { List<DisTask> task = new List<DisTask>() { new DisTask{Name="其它3"}, new DisTask{Name="其它1"}, new DisTask{Name="其它2"}, new DisTask{Name="其它4"}, new DisTask{Name="其它5"}, new DisTask{Name="其它6"}, new DisTask{Name="其它7"}, new DisTask{Name="其它8"}, new DisTask{Name="其它9"}, new DisTask{Name="其它10"}, new DisTask{Name="其它11"} }; int[] ids = {1,2,3}; int eachTotal = task.Count/ids.Length; int remainder = task.Count%ids.Length; int current = 0; for (int i = 0; i < ids.Length; i++) { for (int j = 0; j < eachTotal; j++) { if (current == eachTotal*(i + 1)) { break; } task[current].Id = ids[i]; current++; } } Random rand = new Random(); for (int i = 0; i < remainder; i++) { int d = rand.Next(0, ids.Length); task[eachTotal * ids.Length + i].Id = ids[d]; } Console.WriteLine(remainder); foreach (var i in task) { Console.WriteLine(i.Id+","+i.Name); } } public class DisTask { public int Id { get; set; } public string Name { get; set; } }
新闻热点
疑难解答