Are these random numbers 'safe'
c#, random, security
Solution
Yes it is possible for that to generate the same numbers. The seed adds nothing (it is time based by default anyway).
Also - if it is static, you should synchronize it (`Next` is not thread-safe):
static readonly Random rand = new Random();
public static int NextInt32() {
lock(rand) { return rand.Next();}
}
public static long NextInt64() {
lock(rand) { // using your algorithm...
long randNum = (long)rand.Next() << 33;
randNum |= (uint)rand.Next() << 2;
randNum |= (uint)rand.Next() & 3;
return randNum;
}
}
This can still generate the same number by coincidence of course...
Perhaps consider a cryptographic random number generator if entropy is important.
Problem
I'm not asking if these are truly random. I just wanted to know if two users hit the a page at the same time can they get the same random number? I'm thinking if i run this on a multicore server will i generate the same randon number a good amount of time due to syncing or whatever other reasons? ``` public static class SBackend { static Random randObj = null; public static void init() { randObj = new Random((int)DateTime.Now.ToBinary()); runFirstTime(); } public static long getRandomId() { long randNum = (long)randObj.Next() << 33; randNum |= (uint)randObj.Next() << 2; randNum |= (uint)randObj.Next() & 3; return randNum; } } ```