2010-08-02 85 views
1

我有3個子類「Staff,Faculty,Student」每個類都從父類人員獲取用戶的第一個姓氏,這個id喜歡在控制檯應用程序運行時添加4個隨機數。我遇到的問題是,所有的類都獲得相同的4位數字我該如何解決這個問題,是的,它必須使用隨機我不能使用靜態計數器。消除隨機重複?

Person.cs 「父類」

public class Person 
    { 
     static string title; 
     protected string firstName; 
     protected string lastName; 
     protected string address; 
     protected string gender; 
     protected string dateOfBirth; 
     protected string userID; 
     protected Random rnd = new Random(); 

Faculty.cs

namespace Person 
{ 
    public class Faculty : Person 
    { 
     public Faculty(string aTitle, string aFirstName, string aLastName, string aAddress, 
      string aGender, string aDateOfBirth) 
      : base(aTitle, aFirstName, aLastName, aAddress, 
        aGender, aDateOfBirth) 
     { 


      this.userID = firstName.Substring(0, 1) + lastName.Substring(0, 5); 

      this.userID = this.userID + rnd.Next(1000, 9999); 

      Console.WriteLine(this.userID); 
     } 


    } 
} 

Staff.cs

namespace Person 
{ 
    public class Staff : Person 
    { 
     public Staff(string aTitle, string aFirstName, string aLastName, string aAddress, 
      string aGender, string aDateOfBirth) 
      : base(aTitle, aFirstName, aLastName, aAddress, 
        aGender, aDateOfBirth) 
     { 

      this.userID = firstName.Substring(0, 1) + lastName.Substring(0, 5); 

      this.userID = this.userID + rnd.Next(1000, 9999); 

      Console.WriteLine(userID); 
     } 


    } 
} 

測試類

public class PersonTest 
    { 
     static void Main(string[] args) 
     { 
      Person testPerson = new Faculty(" Mr.", "Merry ", "Lanes", " 493 Bluebane RD", "Male", " 8-06-1953\n "); 


      Person studentPerson = new Student(" Mr.", "Jerry ", "Panes", " 456 Greenbane RD", "Male", " 8-10-1945\n"); 


      Person staffPerson = new Staff(" Mr.", "Joe ", "Gaines", " 495 Redbane RD", "Male", " 8-21-1989\n "); 


      Console.ReadLine(); 
     }//end main 
+4

您的Person實現在哪裏?定義了哪個rnd? – Blorgbeard 2010-08-02 14:20:16

回答

5

我看不到你在哪裏宣佈和初始化rnd。我會猜測它在Person中被聲明爲實例成員,並且在Person的構造函數中初始化 - 總是默認啓動的,這意味着它使用時間作爲種子,這意味着所有的毫秒調用都會得到相同的種子。

使rnd成爲靜態成員,並在靜態ctonstructor中將其初始化一次。

UPDATE:這應該是所有的需要:

static readonly protected Random rnd = new Random(); 

(這也將是更快,因爲你正在創建一個新的隨機對象一次,而不是一次爲每個對象創建從一個種子。時鐘相當慢)

+0

這個固定它保護靜態隨機rnd = new Random(); – 2010-08-02 14:39:01

+0

謝謝大家的解釋,非常感謝 – 2010-08-02 14:44:31

3

如果可以的話,建議重新考慮這一設計

  1. 任何形式的hash值不能保證唯一性
  2. 如果您確實需要獨特的隨機用戶id ,那麼你將需要使用更大的東西,比如GUID,或者保留已經發布的所有UserIds的持久列表。

但是,正如你在你的文章中所建議的,恕我直言的理想方式是使用跟蹤最後發佈的UserId的單例或存儲(例如SQL標識)。

+1

同意:1000-9999,僞隨機,不會激發對唯一性的信心。 – 2010-08-02 14:29:06

+0

我會但這段代碼是從課堂上,並應該完成它的硬件。 – 2010-08-02 14:41:34

0

問題是,我認爲你要緊密連續地初始化你的Random對象,並且因爲系統時鐘具有有限的分辨率,所以對象會得到相同的種子。解決方法是爲所有三個類使用相同的Random對象。

的更多信息:

http://msdn.microsoft.com/en-us/library/system.random.aspx

+0

...但nonnb仍然正確。 – 2010-08-02 14:26:30

1

rnd靜態和立即初始化,這應該是最快的永遠解決您的問題。

據我所知,rnd是基類中的protected/public field/property,因此讓它靜態會在第一次訪問Person類型時創建隨機生成器。

public class Person 
{ 
    protected static Random rnd = new Random(); 
} 

public class PersonImpl : Person 
{ 
    public void DoSomething() 
    { 
     int a = rnd.Next(100, 9999); 
    } 
} 
+0

我不知道OP是否可以使用靜態Random(),如果他不能使用靜態計數器的話。 – 2010-08-02 14:28:39

+0

是的,靜態計數器應該是更好的解決方案,但他說,他需要它是隨機的:) – 2010-08-02 14:31:12

+0

靜態隨機作品,我不能使用靜態計數器原因目前在課堂上使用這個,並與其他人在同一頁我無法偏離那麼多。 – 2010-08-02 14:42:55