Random Number Generator Problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Giovanni said:
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.

You're almost certainly creating a new instance of Random each time,
which is a mistake - you'll end up using the same seed several times,
giving you the same numbers, and therefore the same filename.

Create a single instance of Random and use it repeatedly. Note that
Random isn't thread-safe, so you shouldn't use it from multiple threads
without some sort of locking.

You might want to have a look at my StaticRandom class to simplify
things:

http://www.pobox.com/~skeet/csharp/miscutil
 
Hi Jon,

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
 
Giovanni said:
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?

"lock" is the C# equivalent of SyncLock (and they're statements, not
objects). They do the same thing - syntactic sugar around Monitor.Enter
and Monitor.Exit, basically.
 
Hi Jon,

Thanks for the info. I had justed posted my reply when I discovered
what you stated in your response; I overlooked it. Thanks again for the help.

Regards,

Giovanni
 
Would a GUID be off use?

or even better a SequentialGuid?

public static class SequentialGuids
{
[DllImport("rpcrt4.dll", SetLastError = true)]
static extern int UuidCreateSequential(out Guid guid);

public static Guid NewGuid()
{
Guid rval;
UuidCreateSequential(out rval);
return rval;
}
}

You can also use BITS of this GUID to create a Unique file.

Guids are unique across ALL PCs, so if you just want it unique for your
server you dont need to whole number.

Steve
 
Giovanni,

Steve is right, I always use Guids now when creating files that need
unique naming.

In it's simplest form

// Create the output file
StreamWriter swOut = File.CreateText(folder + "\\" +
Guid.NewGuid().ToString() + ".txt");
 
Back
Top