What algorithm does SQL Server use to generate random numbers for the RAND() function? I believe it is either a linear congruential generator or a mersenne twister, but cannot find any information stating which (or if it is something else entirely)
What algorithm does SQL Server use to generate random numbers for the RAND() function? I believe it is either a linear congruential generator or a mersenne twister, but cannot find any information stating which (or if it is something else entirely)
I wouldn't use RAND. Nowadays I use ABS(CHECKSUM(NEWID())) % 1000 to get random number between 0 and 999. It has better distribution than RAND(). Also RAND() alwasys returns same value first time after SQL Server restart, if I remember correctly.
const double RAND_DIVIDER = 2147483589.4672801884116202;Double numbers are approximate and cannot possibly have more than 15 (sometimes 16) accurate digits, so
double a = 2147483589.4672801884116202; double b = 2147483589.46728017; double c = 2147483589.46728013; Console.WriteLine("a equal to b: " + (a == b).ToString()); Console.WriteLine("b equal to c: " + (b == c).ToString()); Console.WriteLine("a equal to c: " + (a == c).ToString()); Console.ReadLine();The above produces true for all of them.
3 People are following this question.