从一个列表创建两个随机列表 [英] Create two random lists from one list

查看:83
本文介绍了从一个列表创建两个随机列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取一个包含约12个对象的字符串列表,并将其拆分为两个字符串列表,但将其完全随机化.

I want to take a List of strings with around 12 objects and split it into two List of strings but completely randomise it.

列表示例:

清单1:

EXAMPLE 1
EXAMPLE 2
EXAMPLE 3
EXAMPLE 4
EXAMPLE 5
EXAMPLE 6
EXAMPLE 7
EXAMPLE 8

在这里应用一些逻辑...

Apply some logic here...

结果给出了两个列表:清单1:

Result gives me two lists: List 1:

EXAMPLE 5
EXAMPLE 6
EXAMPLE 1
EXAMPLE 8

清单2:

EXAMPLE 2
EXAMPLE 3
EXAMPLE 4
EXAMPLE 7

我是C#MVC的新手,因此我在Stack上找到了一些答案,但没有一个能够回答我的问题.

I'm a newbie to C# MVC, so I've found some answers on Stack but none have been able to answer my question.

到目前为止,我已经尝试过给我一个随机的团队成员.我现在想对此进行扩展,并如上所述创建两个列表.

What I've tried so far gives me one random member of the team. I want to now expand on this and create the two lists as mentioned above.

  [HttpPost]
    public ActionResult Result(Models.TeamGenerator model)
    {
        var FormNames = model.Names;

        string[] lines = FormNames.Split(
            new[] { Environment.NewLine },
            StringSplitOptions.None);

        List<string> listOfLines = new List<string>();

        foreach (var i in lines)
        {
            listOfLines.Add(i);
        }
        string[] result1 = listOfLines.Where(item => item != string.Empty).ToArray();

        Random genRandoms = new Random();
        int aRandomTeam = genRandoms.Next(listOfLines.Count);

        string currName = listOfLines[aRandomTeam];

        return View();
    }

*编辑**感谢您的解决方案!现在,我的应用程序开始运行,并设法将其发布到Web上, https://www.teamgenerator.online .

*EDIT** Thanks for the solution! I've now got my application working and managed to publish it to the web, https://www.teamgenerator.online.

推荐答案

您目前仅生成一个随机数并获得一个值.您需要做的是将其放入循环中,该循环的运行次数是列表中项目的一半.

You're currently only generating one random number and getting one value. What you need to do is put that into a loop that is run half as many times as there are items in the list.

var genRandoms = new Random();
var numberRequired = listOfLines.Count/2;
var output = new List<string>();
for (var i=0; i<numberRequired; i++)
{
    var aRandomTeam = genRandoms.Next(listOfLines.Count);
    output.Add(listOfLines[aRandomTeam]);
    listOfLines.RemoveAt(aRandomTeam);
}

此外,这是开头:

string[] lines = FormNames.Split(
        new[] { Environment.NewLine },
        StringSplitOptions.None);

    List<string> listOfLines = new List<string>();

    foreach (var i in lines)
    {
        listOfLines.Add(i);
    }
    string[] result1 = listOfLines.Where(item => item != string.Empty).ToArray();

可以改写为:

var listOfLines = FormNames.Split(
        new[] { Environment.NewLine },
        StringSplitOptions.RemoveEmptyEntries).ToList();

作为拆分的一部分删除空项目,并使用内置方法将其转换为列表.

Removing the empty items as part of the split, and using a built-in method to convert it to a list.

这篇关于从一个列表创建两个随机列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆