2015-12-10 71 views
2

我有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) 
{ 
    // ???? 
} 
+3

隨機播放列表,並採取第一個50個問題 –

回答

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

我說你可能想要綁定它?'random.Next(min,max)'? – sQuir3l

+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

+0

似乎Linq做得很好! –

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

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

不理論,不要做任何早期的答案。您應該執行Fisher-Yates Shuffle,然後獲取混洗列表的前50個元素。

爲了節省您一些時間,我在下面提供了一個實現。隨機功能是通用的,所以如果以後你決定實際上洗牌的問題(如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; 
     } 
    } 
} 
0

試試這個:

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