2013-08-04 37 views
-3

每當我的應用程序選擇一個「隨機」卡,它總是遵循相同的奇怪模式:俱樂部只有1-3,鑽石4-6,心臟7-9,黑桃10 -12。西裝永遠在這個數字範圍內,永不改變。每個套房應該被允許去13!請幫我解決令人困惑的問題,謝謝!Random.Next不會達到最大參數

來源:

Random Suite = new Random(); 
    Random Value = new Random(); 
    int sprod; 
    int vprod; 

    public Card RandomCardDraw() 
    { 
     sprod = Suite.Next(1, 5); 

     Card newc = new Card(); 

     newc.x = sprod; 

     vprod = Value.Next(1, 13); 

     if (sprod == 1) 
     { 
      newc.suite = CardSuites.Club; 

      newc.y = vprod; 

      switch (vprod) 
      { 
       case 1: 
        newc.cardval = CardValues.Ace; 
        return newc; 
       case 2: 
        newc.cardval = CardValues.Two; 
        return newc; 
       case 3: 
        newc.cardval = CardValues.Three; 
        return newc; 
       case 4: 
        newc.cardval = CardValues.Four; 
        return newc; 
       case 5: 
        newc.cardval = CardValues.Five; 
        return newc; 
       case 6: 
        newc.cardval = CardValues.Six; 
        return newc; 
       case 7: 
        newc.cardval = CardValues.Seven; 
        return newc; 
       case 8: 
        newc.cardval = CardValues.Eight; 
        return newc; 
       case 9: 
        newc.cardval = CardValues.Nine; 
        return newc; 
       case 10: 
        newc.cardval = CardValues.Ten; 
        return newc; 
       case 11: 
        newc.cardval = CardValues.Jack; 
        return newc; 
       case 12: 
        newc.cardval = CardValues.Queen; 
        return newc; 
       case 13: 
        newc.cardval = CardValues.King; 
        return newc; 
      } 
     } 
     if (sprod == 2) 
     { 
      newc.suite = CardSuites.Diamond; 

      newc.y = vprod; 

      switch (vprod) 
      { 
       case 1: 
        newc.cardval = CardValues.Ace; 
        return newc; 
       case 2: 
        newc.cardval = CardValues.Two; 
        return newc; 
       case 3: 
        newc.cardval = CardValues.Three; 
        return newc; 
       case 4: 
        newc.cardval = CardValues.Four; 
        return newc; 
       case 5: 
        newc.cardval = CardValues.Five; 
        return newc; 
       case 6: 
        newc.cardval = CardValues.Six; 
        return newc; 
       case 7: 
        newc.cardval = CardValues.Seven; 
        return newc; 
       case 8: 
        newc.cardval = CardValues.Eight; 
        return newc; 
       case 9: 
        newc.cardval = CardValues.Nine; 
        return newc; 
       case 10: 
        newc.cardval = CardValues.Ten; 
        return newc; 
       case 11: 
        newc.cardval = CardValues.Jack; 
        return newc; 
       case 12: 
        newc.cardval = CardValues.Queen; 
        return newc; 
       case 13: 
        newc.cardval = CardValues.King; 
        return newc; 
      } 
     } 
     if (sprod == 3) 
     { 
      newc.suite = CardSuites.Heart; 

      newc.y = vprod; 

      switch (vprod) 
      { 
       case 1: 
        newc.cardval = CardValues.Ace; 
        return newc; 
       case 2: 
        newc.cardval = CardValues.Two; 
        return newc; 
       case 3: 
        newc.cardval = CardValues.Three; 
        return newc; 
       case 4: 
        newc.cardval = CardValues.Four; 
        return newc; 
       case 5: 
        newc.cardval = CardValues.Five; 
        return newc; 
       case 6: 
        newc.cardval = CardValues.Six; 
        return newc; 
       case 7: 
        newc.cardval = CardValues.Seven; 
        return newc; 
       case 8: 
        newc.cardval = CardValues.Eight; 
        return newc; 
       case 9: 
        newc.cardval = CardValues.Nine; 
        return newc; 
       case 10: 
        newc.cardval = CardValues.Ten; 
        return newc; 
       case 11: 
        newc.cardval = CardValues.Jack; 
        return newc; 
       case 12: 
        newc.cardval = CardValues.Queen; 
        return newc; 
       case 13: 
        newc.cardval = CardValues.King; 
        return newc; 
      } 
     } 
     if (sprod == 4) 
     { 
      newc.suite = CardSuites.Spade; 

      newc.y = vprod; 

      switch (vprod) 
      { 
       case 1: 
        newc.cardval = CardValues.Ace; 
        return newc; 
       case 2: 
        newc.cardval = CardValues.Two; 
        return newc; 
       case 3: 
        newc.cardval = CardValues.Three; 
        return newc; 
       case 4: 
        newc.cardval = CardValues.Four; 
        return newc; 
       case 5: 
        newc.cardval = CardValues.Five; 
        return newc; 
       case 6: 
        newc.cardval = CardValues.Six; 
        return newc; 
       case 7: 
        newc.cardval = CardValues.Seven; 
        return newc; 
       case 8: 
        newc.cardval = CardValues.Eight; 
        return newc; 
       case 9: 
        newc.cardval = CardValues.Nine; 
        return newc; 
       case 10: 
        newc.cardval = CardValues.Ten; 
        return newc; 
       case 11: 
        newc.cardval = CardValues.Jack; 
        return newc; 
       case 12: 
        newc.cardval = CardValues.Queen; 
        return newc; 
       case 13: 
        newc.cardval = CardValues.King; 
        return newc; 
      } 
     } 
     else 
     { 
      return newc; 
     } 
     return newc; 
    } 
+1

如果甲板上有1000張卡片,你會怎麼做? – I4V

+0

這是處於非常早期的發展階段,我計劃在稍後實施更優雅的系統:) – user2649633

+0

即使按照您的預期工作,「.Next(1,13)」永遠不會返回13. –

回答

1

您正在運行到上,很多人有Random問題稍有不同。默認構造函數使用基於Environment.TickCount的種子進行初始化。如果您快速創建兩個實例,它們將以相同的種子結束,因爲系統計時器尚未更新。因此這兩個實例都產生相同的數字序列。 Next(int,int)做了一些由受保護的Sample方法生成的double的簡單縮放比例。只需使用一個隨機實例並調用Next(1,5)Next(1,13)即可。

+0

謝謝你的回答 – user2649633

+0

@ user2649633它實際上應該是'Next(1,14)'來得到範圍1 - 13 –

0

如果CardValuesCardSuites是枚舉,所有的代碼,可以有效地替換

static Random random = new Random(); 

static Card RandomCardDraw() 
{ 
    var suite = random.Next(5); 
    var value = random.Next(14); 
    return Card 
    { 
      x = suite + 1, 
      y = value + 1, 
      suite = (CardSuites)suite, 
      cardval = (CardValues)value, 
    }; 
} 

注:

你只需要一個Random實例。同時創建的兩個實例將具有相同的種子併產生相關序列(如果提供相同的參數,則產生相同的序列)。

最後一個參數Random.Next()獨家

+0

好的建議,我對編程非常陌生,所以我不完全理解所提出的概念,但它比我累贅的200多行代碼好多了!謝謝! – user2649633

+0

更正爲random.Next(5)和random.Next(14)。當使用單個參數時,最大值也是獨佔的。 –

+0

其實你也不需要兩個'Next'。 'var rCard = random.Next(52); int suite = rCard/13; int cardVal = rCard%13;' –