从列表对象中选择随机项 [英] Choose random item from a list object

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

问题描述





我正在玩Lists,我想知道如何显示该列表中的随机项目/元素。



我有一个名为Person的班级



公共班级人员
{
public string Name {get;组; }
公共字符串姓氏{get;组; }
}





在我的表单中,我创建了两个文本框(txtName& txtSurname)我想要实现的是每当用户单击一个按钮(btnShow)它将自动显示名称或姓氏(从不两者)。



我创建了一个包含Person对象和初始化Person对象的List。

 私人列表< Person> people =  new  List(); 
私人个人;





这是我的btnCreate事件。这会在列表中添加新人:



  private   void  btnShow_Click( object  sender,EventArgs e){
person = new Person();
person.Name = txtName.Text;
country.Surname = txtSurname.Text;
countries.Add(country);
}





我的尝试:



这是我的btnShow事件,它将显示List<>文本框中的项目。请记住,我希望每次都不显示姓名或姓氏。我不知道该怎么做。我试图简单地在整个列表中随机播放以显示某些内容,但它显示的是例如System.Random而不是实际名称。



  private   void  btnShow_Click( object  sender,EventArgs e){
Random r = new Random ();
string random = people [r.Next(people.Count)]。ToString();

txtName.Text = random.ToString();
}





任何帮助将不胜感激



谢谢。

解决方案

不起作用并没有给任何人足够的信息来帮助你,你发生了什么,你没想到会发生什么,或者是什么发生了你想要发生的事情吗?



我将假设你的问题部分是在Person对象上使用ToString。如果你不覆盖ToString,你只需要获得该类型的名称。所以要么明确你想要的结果如何;



 string random = people [r.Next(people.Count)]; 
string result = random.Surname +,+ random.Name;





或覆盖Person类上的ToString返回你想要的结果。如果结果必须是随机名称或姓氏,你可以做类似的事情



公共字符串ToString(随机随机)
{
if(random.Next(2)== 0)
{
return this.Name;
}
其他
{
返回此姓。
}
}

公共覆盖字符串ToString()
{
返回ToString(new Random());
}





我做了一个标准的ToString(),还有一个你可以传入你自己的Random实例的地方。这是为了您想要在循环中运行代码。随机使用时间作为其随机数生成器的种子,因此如果在循环中创建Random的新实例,则每次最终都会使用相同的随机数,因为没有足够的时间来生成新的seed \ sequence。


Hi,

I am playing around with Lists and I was wondering how to show a random item/element from that List.

I have a class called Person

public class Person
    {
        public string Name { get; set; }
        public string Surname { get; set; }
    }



In my form I have created two textboxes (txtName & txtSurname) what I want to achieve is whenever user clicks a button (btnShow) it will automatically display either the name or the surname (never both).

I have created a List that hold the Person Object and initialized Person object.

private List<Person> people = new List();
private Person person;



This is my btnCreate event. Which adds new people to the list:

private void btnShow_Click(object sender, EventArgs e){
person= new Person();
person.Name= txtName.Text;
country.Surname= txtSurname.Text;
countries.Add(country);
}



What I have tried:

This is my btnShow event which will show the List<> items in the text boxes. Keep in my mind that I want to also display only the name or surname each time not both. And I am not sure how to do it. I have tried to just simply shuffle through the whole list to get something displayed but it displayed for example, System.Random instead of actual name.

private void btnShow_Click(object sender, EventArgs e){
Random r = new Random();
string random = people[r.Next(people.Count)].ToString();

txtName.Text = random.ToString();
}



Any help would be appreciated

Thanks.

解决方案

"Doesn't work" doesn't give anyone enough information to help you, what is happening that you didn't expect to happen, or what isn't happening that you want to happen?

I'm going to assume your issue is partly in using ToString on the Person object. If you don't override ToString you'll just get the name of the type. So either be explicit in how you want the result to look;

string random = people[r.Next(people.Count)];
string result = random.Surname + ", " + random.Name;



or override ToString on the Person class to return the result you want. If the result has to be random Name or Surname you can do something like

public string ToString(Random random)
{
    if (random.Next(2) == 0)
    {
        return this.Name;
    }
    else
    {
        return this.Surname;
    }
}

public override string ToString()
{
    return ToString(new Random());
}



I've done a standard ToString() and also one where you can pass in your own instance of Random. This is in case you want to run the code in a loop. Random uses the time as a seed for its random number generator so if you create new instances of Random in a loop you can end up with the same random number each time because enough time hasn't passed to generate a new seed\sequence.


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

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