开发者

Random Name Picker

开发者 https://www.devze.com 2023-01-03 23:17 出处:网络
I have a class which contains 10 names (U1, U2, U3..and so on). I have to choose 5 names everyday, and display one as the Editor and 4 as Contributors

I have a class which contains 10 names (U1, U2, U3..and so on). I have to choose 5 names everyday, and display one as the Editor and 4 as Contributors

While selecting the random names, I have to also consider that if one user is selected as Editor, he cannot become editor again till everyone got their chance.

The output should look similar to the following:

           Editor   Cont1   Cont2     Cont3    Cont4
20-Jun   U1      U8       U9         U3       U4
21-Jun    U7    开发者_运维知识库  U2       U5         U6       U10
22-Jun    U3      U4       U9         U2       U8
23-Jun      U4      U8       U3          U5      U2
and so on..

Can I do this in LINQ?


I would not use Linq, but instead use the Random class provided by the .Net Framework.

Random random = new Random();
List<Person> tmpList = new List<Person>(personList);
int item = random.Next(tmpList.Count);
Person editor = tmpList[item];
tmpList.RemoveAt(item);
List<Person> contributors = new List<Person>();
for(int i = 0; i < 5; i++)
{
     item = random.Next(tmpList.Count);
     contributors.Add(tmpList[item]);
     tmpList.RemoveAt(item);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消