C# 从mvc中的列表中随机获取10个用户数据

C# 从mvc中的列表中随机获取10个用户数据,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个包含所有用户信息的对象列表 List<UsrProfile> listUsr = GetAllUser(); List listUsr=GetAllUser(); 在仪表板中,每次页面刷新时,我只能从列表中随机显示10个用户配置文件。这应该可以满足您的要求: 它会创建一个临时列表,以确保您不会随机选择已选择的重复项,并假设该列表至少包含10个元素 Random rnd = new Random(); var tempList = new List<User>

我有一个包含所有用户信息的对象列表

List<UsrProfile> listUsr = GetAllUser();
List listUsr=GetAllUser();

在仪表板中,每次页面刷新时,我只能从列表中随机显示10个用户配置文件。

这应该可以满足您的要求: 它会创建一个临时列表,以确保您不会随机选择已选择的重复项,并假设该列表至少包含10个元素

Random rnd = new Random();

var tempList = new List<User>(listUsr);

int count = 10;

while (count > 0)
{
    int r = rnd.Next(tempList.Count);
    // Do whatever with tempList[r] 
    templist.RemoveAt(r);
    count--;
}
Random rnd = new Random();
List<User> resultList = new List<User>();

while (resultList.Count < 10)
{
    User u = listUsr[rnd.Next(listUsr.Count)];
    if (!resultList.Contains(u))
    {
        resultList.Add(u);
    }
}
Random rnd=new Random();
var templast=新列表(listUsr);
整数计数=10;
而(计数>0)
{
int r=rnd.Next(templast.Count);
//对圣殿骑士做任何事[r]
圣殿骑士。RemoveAt(右);
计数--;
}

这应该满足您的要求: 它会创建一个临时列表,以确保您不会随机选择已选择的重复项,并假设该列表至少包含10个元素

Random rnd = new Random();

var tempList = new List<User>(listUsr);

int count = 10;

while (count > 0)
{
    int r = rnd.Next(tempList.Count);
    // Do whatever with tempList[r] 
    templist.RemoveAt(r);
    count--;
}
Random rnd = new Random();
List<User> resultList = new List<User>();

while (resultList.Count < 10)
{
    User u = listUsr[rnd.Next(listUsr.Count)];
    if (!resultList.Contains(u))
    {
        resultList.Add(u);
    }
}
Random rnd=new Random();
var templast=新列表(listUsr);
整数计数=10;
而(计数>0)
{
int r=rnd.Next(templast.Count);
//对圣殿骑士做任何事[r]
圣殿骑士。RemoveAt(右);
计数--;
}

我更喜欢使用LINQ,它简短易读

var r = new Random(); 
var tenRandomUser = listUsr.OrderBy(u => r.Next()).Take(10);

我更喜欢使用LINQ,简短易读

var r = new Random(); 
var tenRandomUser = listUsr.OrderBy(u => r.Next()).Take(10);
你可以用两种方式来做(好的,也许更多,但我想到了两种)。 一个是@wentimo所描述的。 第二种方法是创建结果列表,并从源列表中随机选择,如果还没有,则添加结果:


这应该可以满足您的要求:它创建一个临时列表,以确保您不会随机选择已选择的重复项,并且还假设该列表至少包含10个元素

Random rnd = new Random();

var tempList = new List<User>(listUsr);

int count = 10;

while (count > 0)
{
    int r = rnd.Next(tempList.Count);
    // Do whatever with tempList[r] 
    templist.RemoveAt(r);
    count--;
}
Random rnd = new Random();
List<User> resultList = new List<User>();

while (resultList.Count < 10)
{
    User u = listUsr[rnd.Next(listUsr.Count)];
    if (!resultList.Contains(u))
    {
        resultList.Add(u);
    }
}
Random rnd=new Random();
列表结果列表=新列表();
而(resultList.Count<10)
{
用户u=listUsr[rnd.Next(listUsr.Count)];
如果(!resultList.Contains(u))
{
结果列表添加(u);
}
}
什么时候应该用哪种方式? 若随机元素的数量远远少于源列表中的元素数量,那个么使用第二种方法。例如,如果您从100000个用户中选择10个,您将很少两次获得同一个用户,并且检查结果列表是否包含元素对于少量元素来说是快速的

Random rnd = new Random();

var tempList = new List<User>(listUsr);

int count = 10;

while (count > 0)
{
    int r = rnd.Next(tempList.Count);
    // Do whatever with tempList[r] 
    templist.RemoveAt(r);
    count--;
}
Random rnd = new Random();
List<User> resultList = new List<User>();

while (resultList.Count < 10)
{
    User u = listUsr[rnd.Next(listUsr.Count)];
    if (!resultList.Contains(u))
    {
        resultList.Add(u);
    }
}
另一方面,您正在避免再创建一个包含100000个元素的列表。若随机元素的数量接近于源元素的数量,那个么使用第一种方法,因为您通常会选择两次或多次相同的用户,结果列表将检查更多元素的存在性

Random rnd = new Random();

var tempList = new List<User>(listUsr);

int count = 10;

while (count > 0)
{
    int r = rnd.Next(tempList.Count);
    // Do whatever with tempList[r] 
    templist.RemoveAt(r);
    count--;
}
Random rnd = new Random();
List<User> resultList = new List<User>();

while (resultList.Count < 10)
{
    User u = listUsr[rnd.Next(listUsr.Count)];
    if (!resultList.Contains(u))
    {
        resultList.Add(u);
    }
}
此外,如果不确定源元素数量和随机元素数量之间的关系,请选择第一个(@wentimo)解决方案。

您可以用两种方法来实现(好的,可能更多,但想到两种)。 一个是@wentimo所描述的。 第二种方法是创建结果列表,并从源列表中随机选择,如果还没有,则添加结果:


这应该可以满足您的要求:它创建一个临时列表,以确保您不会随机选择已选择的重复项,并且还假设该列表至少包含10个元素

Random rnd = new Random();

var tempList = new List<User>(listUsr);

int count = 10;

while (count > 0)
{
    int r = rnd.Next(tempList.Count);
    // Do whatever with tempList[r] 
    templist.RemoveAt(r);
    count--;
}
Random rnd = new Random();
List<User> resultList = new List<User>();

while (resultList.Count < 10)
{
    User u = listUsr[rnd.Next(listUsr.Count)];
    if (!resultList.Contains(u))
    {
        resultList.Add(u);
    }
}
Random rnd=new Random();
列表结果列表=新列表();
而(resultList.Count<10)
{
用户u=listUsr[rnd.Next(listUsr.Count)];
如果(!resultList.Contains(u))
{
结果列表添加(u);
}
}
什么时候应该用哪种方式? 若随机元素的数量远远少于源列表中的元素数量,那个么使用第二种方法。例如,如果您从100000个用户中选择10个,您将很少两次获得同一个用户,并且检查结果列表是否包含元素对于少量元素来说是快速的

Random rnd = new Random();

var tempList = new List<User>(listUsr);

int count = 10;

while (count > 0)
{
    int r = rnd.Next(tempList.Count);
    // Do whatever with tempList[r] 
    templist.RemoveAt(r);
    count--;
}
Random rnd = new Random();
List<User> resultList = new List<User>();

while (resultList.Count < 10)
{
    User u = listUsr[rnd.Next(listUsr.Count)];
    if (!resultList.Contains(u))
    {
        resultList.Add(u);
    }
}
另一方面,您正在避免再创建一个包含100000个元素的列表。若随机元素的数量接近于源元素的数量,那个么使用第一种方法,因为您通常会选择两次或多次相同的用户,结果列表将检查更多元素的存在性

Random rnd = new Random();

var tempList = new List<User>(listUsr);

int count = 10;

while (count > 0)
{
    int r = rnd.Next(tempList.Count);
    // Do whatever with tempList[r] 
    templist.RemoveAt(r);
    count--;
}
Random rnd = new Random();
List<User> resultList = new List<User>();

while (resultList.Count < 10)
{
    User u = listUsr[rnd.Next(listUsr.Count)];
    if (!resultList.Contains(u))
    {
        resultList.Add(u);
    }
}

此外,如果不确定源元素数量和随机元素数量之间的关系,请选择第一个(@wentimo)解决方案。

您可以使用下面的代码行-


listUsr.Take(10)

您可以使用下面的代码行-


listUsr.Take(10)

按随机顺序排序,取前十个值。t按随机顺序排序,取前十个值。这很有效,但要小心。如果您连续多次调用它(例如,在一个紧循环中),您可能会发现您多次得到相同的数字,因为随机数生成器是用相同的种子初始化的。最好在构造函数中创建一个持久的
Random
实例,然后每次调用获取随机项的方法时都使用它。是的,
Random
的静态实例可能会有帮助,但要小心。如果您连续多次调用它(例如,在一个紧循环中),您可能会发现您多次得到相同的数字,因为随机数生成器是用相同的种子初始化的。最好在构造函数中创建一个持久的
Random
实例,然后每次调用获取随机项的方法时都使用它。是的,
Random
的静态实例可能会有所帮助