none
Random Number Generator Problem

    Question

  • Hi all,

         I cannot seem to solve this problem:  I am attempting to generate a unique filename with the help of the Random Number Generator (RND.Next.ToString).  All seems to work fine except that one third of the time, I get duplicate file names; this poses a critical problem for my application.  The problem only seems to happen when Method1 calls Function2 to generate and return the random file name.  If I move the logic to generate the filename into Method1, it works properly.  I have tried to use SyncLock, etc. but to no avail.

         Why is it that when Method1 calls Function2 to generate the random file name, I get duplicates, whereas if I move the logic from Function2 into Method1, I get no duplicate file names.  Is this due to threading?  I am lost as to how to solve this seemingly simple problem.

         Note:  I have also tried the above replacing the Random Number Generator with the Now.Second and Now.MilliSecond but with the exact same results.  It only works if it is done within the same method that is doing my actual processing.

         Regards,

              Giovanni

    Sunday, January 21, 2007 8:41 AM

Answers

  • Hard to tell without seeing source code.  Wild guess: Function2 creates an instance of the Random class.  That causes it to generate the same random sequence again if Function2 was called within 15 milliseconds.  Move the Random class reference to class scope so it gets initialized only once.

    Or use System.IO.Path.GetTempFileName().  It ensures you'll get a truly unique filename; no such guarantees if you use Random to make you own.
    Sunday, January 21, 2007 12:05 PM
  • Not sure what you mean.  SyncLock is a Visual Basic keyword, lock is a C# keyword.  They do the exact same thing.
    Sunday, January 21, 2007 5:41 PM

All replies

  • Hard to tell without seeing source code.  Wild guess: Function2 creates an instance of the Random class.  That causes it to generate the same random sequence again if Function2 was called within 15 milliseconds.  Move the Random class reference to class scope so it gets initialized only once.

    Or use System.IO.Path.GetTempFileName().  It ensures you'll get a truly unique filename; no such guarantees if you use Random to make you own.
    Sunday, January 21, 2007 12:05 PM
  • Hi,

         Thanks for the info.  I really appreciate it.  I will try to implement your solution.  That aspect of the Random Number Generator had eluded me.  Is there a difference between using the Lock and the SyncLock objects?  Is one better than the other?


         Beat Regards,

             Giovanni

    Sunday, January 21, 2007 5:33 PM
  • Not sure what you mean.  SyncLock is a Visual Basic keyword, lock is a C# keyword.  They do the exact same thing.
    Sunday, January 21, 2007 5:41 PM
  • Hi,

         Sorry about that one.  I had posted my reply before reading up on them both and only then noticed the keyword difference between the two languages.  Thanks again for your help.

         Regards,

               Giovanni

    Sunday, January 21, 2007 5:46 PM