2010-08-02 72 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