简体   繁体   English

从列表中选择不同的随机数

[英]Select distinct and random numbers from list

I have list of 70 question numbers out of which I want to select 50 random and unique questions. 我有70个问题编号的列表,我想从中选择50个随机和唯一的问题。 How can I do this? 我怎样才能做到这一点?

Here is what I have so far: 这是我到目前为止的内容:

static Random random = new Random();

static void Main(string[] args)
{
    List<int> questionNumbers = new List<int>();
    for (int i = 0; i < 70; i++)
    {
        questionNumbers.Add(i);
    }

    List<int> randomAndUniqueNumbers = GenerateRandom(50);
}

public static List<int> GenerateRandom(int count)
{
    // ????
}

Try using extension methods of Linq. 尝试使用Linq的扩展方法。

class Program
{
    static void Main(string[] args)
    {
        List<int> questionNumbers = new List<int>();
        for (int i = 0; i < 70; i++)
        {
            questionNumbers.Add(i);
        }

        List<int> randomAndUniqueNumbers = questionNumbers.GenerateRandom(50);
    }


}

public static class Extensions
{
    static Random random = new Random();
    public static List<T> GenerateRandom<T>(this List<T> collection, int count)
    {
        return collection.OrderBy(d => random.Next()).Take(count).ToList();
    }
}

Dot net fiddle is here 点网提琴在这里

Try this 尝试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication61
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
    public class Question
    {
        private static Random rand = new Random();
        public static List<Question> questions { get; set; }
        public string question { get; set; }
        public int randomNumber { get; set; }

        public void Shuffle()
        {
            foreach(Question question in questions)
            {
                question.randomNumber = rand.Next();
            }
            questions = questions.OrderBy(x => x.randomNumber);
        }

    }
}
    static Random random = new Random();
    static List<int> questionNumbers = new List<int>();

    static void Main(string[] args)
    {
        for (int i = 0; i < 70; i++)
        {
            questionNumbers.Add(i);
        }

        List<int> randomAndUniqueNumbers = GenerateRandom(50);
    }

    //This function doesn't require index in questionNumbers
    //to be from 0 and continuous
    public static List<int> GenerateRandom(int count)
    {
        List<int> lst = new List<int>();
        List<int> q = questionNumbers.ToList();
        for (int i = 0; i < count; i++)
        {
            int index = random.Next(0, q.Count);
            lst.Add(q[index]);
            q.RemoveAt(index);
        }
        return lst.OrderBy(x => x).ToList();
    }

Well, just remove the taken question (or its index) from the list: 好吧,只需从列表中删除采取的问题 (或其索引):

static Random random = new Random();

private static IEnumerable<int> GenerateRandom(int takeCount, int questionCount) {
  List<int> numbers = Enumerable
    .Range(0, questionCount)
    .ToList();

  for (int i = 0; i < takeCount; ++i) {
    int index = random.Next(numbers.Count);

    yield return numbers[index];

    numbers.RemoveAt(index);
  }
}

static void Main(string[] args) {
   List<int> randomAndUniqueNumbers = GenerateRandom(50, 70).ToList();
}

Without getting into theory, DO NOT do any of the earlier answers. 不用理论,不要做任何较早的答案。 You should be implementing a Fisher-Yates Shuffle and then take the first 50 elements of the shuffled list. 您应该实现Fisher-Yates随机播放 ,然后采用随机播放列表的前50个元素。

To save you some time I've put an implementation below. 为了节省您的时间,我在下面放置了一个实现。 The Shuffle function is generic so if later you decide to actually shuffle questions (eg a List<Question> instead of numbers it will still work for you: 随机播放功能是通用的,因此如果以后您决定实际随机播放问题(例如, List<Question>而不是数字,它将仍然对您有效:

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        List<int> numbers = Enumerable.Range(1, 70).ToList();
        Shuffle(numbers);
        foreach (int number in numbers.Take(50))
        {
            Console.WriteLine(number);
        }
        Console.ReadLine();
    }

    static void Shuffle<T>(IList<T> items)
    {
        Random rand = new Random();
        for (int i = items.Count - 1; i > 0 ; i--)
        {
            int j = rand.Next(i + 1); // Returns a non-negative random integer that is less than the specified maximum - MSDN

            T temp = items[i];
            items[i] = items[j];
            items[j] = temp;
        }
    }
}

Try this: 尝试这个:

List<int> randomAndUniqueNumbers =
    Enumerable
        .Range(0, 70)
        .OrderBy(x => random.Next())
        .Take(50)
        .ToList();

This should do it. 这应该做。 You might want to bound the random number by putting in a min and max into random.Next(min, max) 您可能需要通过将min和max放入random.Next(min, max)来限制随机数。

public static List<int> GenerateRandom(int count)
{
    var result = new HashSet<int>();
    while (result.Count < 50)
    {
        result.Add(random.Next());
    }

    return result.ToList();
}

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

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