views:

52

answers:

2

From what i know Random() is initialize to the current time. If two connections hit during the same second i should get the same two random numbers? With a large site that can be likely. Locking is bad so how should i solve it? note: the number is used for the session id.

-edit- i am stuck using a long. It feels wrong to shorten a 128bit GUID

+4  A: 

Rather than using a random number from a call to Random(), use a Guid.NewGuid(). The chance of a duplicate is very, very small...

Using a GUID for a session ID is a common solution.

Mitch Wheat
Specifically, Guid.NewGuid(), (otherwise you could actually construct duplicate guids). Guid.NewGuid() is essentially a psuedo-random number generator
matt-dot-net
would Convert.ToInt64(guid); be fine?
acidzombie24
Would the downvoter please leave a comment. Thanks.
Mitch Wheat
+1 for simplicity.
rockinthesixstring
i did leave a comment - essentially what you said was "Don't use a random, use a random"
matt-dot-net
@matt-dot-net: a random number and a GUID are different things...
Mitch Wheat
Yes, they are two different things entirely, exactly the point I have been trying to make to you. One is a concept and the other is a value data type. But, when you mention Guid.NewGuid() then you are generating a pseudo random number and then the only difference is size of the data. Your edited answer makes more sense now. +1
matt-dot-net
+2  A: 

First, why do you need to create your own session id? Are you using SessionIDManager ? I would use the Guid.NewGuid() method like what is shown in in the example CreateSessionID . According to the documentation for Guid.NewGuid(), "The chance that the value of the new Guid will be all zeros or equal to any other Guid is very low."

matt-dot-net