2012-05-20 47 views
0

如何在C#中不重複生成隨機數字。我有一個數組,我想用0到9的隨機數填充每個房間。每個房間都有不同的數字。我使用這個:在c中生成隨機數字不重複#

for (int i = 0; i < 20; i++) 
{ 
    Random rnd = new Random(); 
    int temp = 0; 
    temp = rnd.Next(0, 9); 
    page[i] = temp; 
} 

但是我在evey房間的數組中得到相同的數字。

+1

「我只想知道如何在c#中不重複生成隨機數?」 - 這不可能。假定一個隨機數重複,否則它實際上不是一個隨機數發生器,而是一個唯一的值生成器,它是一個可以編碼的單值。您的代碼也存在缺陷,因爲您始終擁有相同的種子值。 –

+0

你談論房間,但有一個頁面陣列!?你的意思是數組頁面中的每個條目都應該是唯一的嗎?我理解你是對的,你需要20個數字在頁面中,都是不同的? –

回答

5

有了這樣一個小號的數字列表供您選擇,您可以簡單地生成一個列表,其中包含所有這些列表,然後shuffle它們。

3

你的問題是你在每個循環中創建Random對象。 Random對象只能創建一次。嘗試這個代替:

Random rnd = new Random(); // <-- This line goes out of the loop   
for (int i = 0; i < 20; i++) { 
    int temp = 0; 
    temp = rnd.Next(0, 9); 
    page[i] = temp; 
} 
-2

100%右

public int[] UniqeRandomArray(int size , int Min , int Max) { 

     int [] UniqueArray = new int[size]; 
     Random rnd = new Random(); 
     int Random; 

     for (int i = 0 ; i < size ; i++) { 

      Random = rnd.Next(Min, Max); 

      for (int j = i; j >= 0 ; j--) { 

       if (UniqueArray[j] == Random) 
       { Random = rnd.Next(Min, Max); j = i; } 

      } 

      UniqueArray[i] = Random; 

     } 

     return UniqueArray; 

    } 

//通知是獨特的[最大值 - 最小值>尺寸] NOT等於

+0

另外,這對於大型數組並不是很有效,您可以創建一個排序數組並使用一種混洗技術來創建隨機性 – Anoush

-1
 ArrayList page=new ArrayList(); 
     int random_index;    
     random rnd = new Random(); 

     for (int i = 0; i < 20; i++) 
     { 
      do        
      { 
       random_index = rnd.Next(10); 
       if (!(page.Contains(random_index))) 
           break; 
      } while (page.Contains(random_index)); 
      page.Add(random_index); 
     } 
+0

你能解釋一下你的答案嗎? – Benvorth

-1
public Form1() 
    { 
     InitializeComponent(); 
    } 
    int A, B; 
    string Output; 
    private void Form1_Load(object sender, EventArgs e) 
    { 
     for (int i = 0; i < 20; i++) 
     { 
      while (A == B) 
      { 
       Random r = new Random(); 
       A = r.Next(1, 6); 
      } 
      Output = Output + A; 
      B = A; 
     } 
     textBox1.Text = Output; 
    } 

輸出:24354132435213245415(不重複)

-1
using System; 

using System.Collections.Generic; 

using System.Linq; 


namespace nonRepeatableRndm 
{ 

    class Program 
    { 
     //variable with the Values 
     List<string> RandomVal = new List<string>(); 
     //variable to compare the randomly genarated Values 
     List<string> CompaerbyString = new List<string>(); 
     //Variable that gets Value from the list Values 
     string DisplayVal; 
     //instantiates the Random Class 
     Random r; 
     //this Method gives Value to the list and initializes th the Random Class 
     void setVal() 
     { 
      //Adding to the list 
      RandomVal.Add("A"); 
      RandomVal.Add("b"); 
      RandomVal.Add("c"); 
      RandomVal.Add("d"); 
      RandomVal.Add("e"); 
      RandomVal.Add("f"); 
      RandomVal.Add("g"); 
      RandomVal.Add("h"); 
      RandomVal.Add("i"); 
      RandomVal.Add("j"); 
      RandomVal.Add("k"); 
      RandomVal.Add("l"); 
      RandomVal.Add("m"); 
      RandomVal.Add("n"); 
      RandomVal.Add("o"); 
      RandomVal.Add("p"); 
      RandomVal.Add("q"); 
      RandomVal.Add("r"); 
      RandomVal.Add("s"); 
      RandomVal.Add("t"); 
      RandomVal.Add("u"); 
      RandomVal.Add("v"); 
      RandomVal.Add("w"); 
      RandomVal.Add("x"); 
      RandomVal.Add("y"); 
      RandomVal.Add("z"); 

      //Instantiating the Random Method 
      r = new Random(); 
     } 
     //This method Gives Out the Random Values 
     public void DisplayRand() 
     { 

      //Setting Random Index 
      int getIndex = r.Next(0, RandomVal.Count - 1); 
      //Now we are trying to pass a random value to the String 
      DisplayVal = RandomVal.ElementAt<string>(getIndex); 
      //we are testing to see if String in Display is contained in the List that will used Compare 
      if (!CompaerbyString.Contains(DisplayVal)) 
       Console.WriteLine(DisplayVal.ToUpper()); 
      else 
      { 
       try 
       { 
        this.DisplayRand(); 
       } 
       catch(Exception e) 
       { 
        Console.WriteLine("You have Reached the End of the list..."); 
        Environment.Exit(0); 
       } 
      } 
      //Adding Corrent DisplayVal's Value to the List for Comparison 
      CompaerbyString.Add(DisplayVal); 
     } 
     //This is Simple method that Calls the Display 
     void Call() 
     { 
      //This For loop is to Ensure we have no Stack Overflow 
      for (int i = 0; i < RandomVal.Count-1;i++) 
      { 
       this.DisplayRand(); 

      } 
     } 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Random Values With Out Repeatating Any Value"); 
      //Simple Instantiation 
      Program dis = new Program(); 
      //Simple Call 
      dis.setVal(); 
      //Simple Call 
      dis.Call() ; 
      Console.ReadLine(); 
     } 
    } 
} 

This is the output

+0

我希望這有助於 –

0

這將創建1獨特的範圍rangeEx包容性。接下來的兩行創建一個隨機數生成器,並用一個隨機數命令IEnumerable範圍。然後用ToArray調用它並返回!

private int[] RandomNumber(int rangeEx) 
    { 
     var orderedList = Enumerable.Range(1, range); 
     var rng = new Random(); 
     return orderedList.OrderBy(c => rng.Next()).ToArray(); 
    }