简体   繁体   English

如何从列表中获取随机数的元素

[英]How can I get a random number of element from list

I am writing to You with small problem. 我给你写信有小问题。 I am writing small application in C#.NET MVC5, and I have one question, how can I get a few random items from List? 我正在C#.NET MVC5中编写小型应用程序,我有一个问题,如何从列表中获得一些随机项?

My code: 我的代码:

public ActionResult ProductsList()
{
    List<Product> products = productRepo.GetProduct().ToList();
    return PartialView(products);
}

This method return full list, how can I do it correctly? 此方法返回完整列表,如何正确执行?

I suggest selecting out random indexes and then returning corresponding items: 我建议选择随机索引,然后返回相应的项目:

// Simplest, not thread-safe
private static Random s_Random = new Random();

private static List<Product> PartialView(List<Product> products, int count = 6) {
  // Too few items: return entire list
  if (count >= products.Count)
    return products.ToList(); // Let's return a copy, not list itself

  HashSet<int> taken = new HashSet<int>();

  while (taken.Count < count)
    taken.Add(s_Random.Next(count));

  List<Product> result = new List<Product>(count);

  // OrderBy - in case you want the initial order preserved
  foreach (var index in taken.OrderBy(item => item))
    result.Add(products[index]);

  return result;
}

Generate a random no and fetch the 6 elements of the list based on the random No. generated. 生成一个随机编号,并根据生成的随机编号获取列表的6个元素。

    public ActionResult ProductsList()
    {
        Random rnd = new Random();
        List<Product> products = productRepo.GetProduct().ToList();
        Random r = new Random();
        int randomNo = r.Next(1, products.Count);
        int itemsRequired = 6; 
        if (products.Count <= itemsRequired)
            return PartialView(products));
        else if (products.Count - randomNo >= itemsRequired)
            products = products.Skip(randomNo).Take(itemsRequired).ToList();
        else
            products = products.Skip(products.Count - randomNo).Take(itemsRequired).ToList();
        return PartialView(products));

Create an instance of Random class somewhere. 在某个地方创建一个Random类的实例。 Note that it's pretty important not to create a new instance each time you need a random number. 请注意,每次需要随机数时都不要创建新实例,这一点非常重要。 You should reuse the old instance to achieve uniformity in the generated numbers. 您应该重用旧实例以实现生成数字的一致性。

static Random rnd = new Random();
List<Product> products = productRepo.GetProduct().ToList();
int r = rnd.Next(products.Count);
products = products.Take(r).ToList();

Make use of the Random class and make use of NEXT function which returns the non negative number below the specified limit... 利用Random类并利用NEXT函数,该函数返回低于指定限制的非负数...

List<Product> products = productRepo.GetProduct().ToList();

var randomProduct=new Random();

var index=randomProduct.Next(products.Count);

return PartialView(products[index]);

Hope this could help you.. Happy coding 希望这可以对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM