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

时间:2015-12-10 05:43:14

标签: c#

我有70个问题编号列表,其中我想选择50个随机和独特的问题。我怎么能这样做?

这是我到目前为止所做的:

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)
{
    // ????
}

7 个答案:

答案 0 :(得分:3)

尝试使用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();
    }
}

点网小提琴是here

答案 1 :(得分:0)

试试这个

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);
        }

    }
}

答案 2 :(得分:0)

    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();
    }

答案 3 :(得分:0)

好吧,只需从列表中删除带问题的(或其索引):

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();
}

答案 4 :(得分:0)

不进入理论,不要做任何早期的答案。您应该实现Fisher-Yates Shuffle,然后获取混洗列表的前50个元素。

为了节省你一些时间,我在下面提出了一个实现。 Shuffle功能是通用的,所以如果以后你决定实际改变问题(例如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;
        }
    }
}

答案 5 :(得分:0)

试试这个:

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

答案 6 :(得分:-3)

这应该这样做。您可能希望通过将最小值和最大值放入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();
}