Will Rand(x) always generate the same number for the same value of x on any SQL Server?

4c8rllxm  于 2023-11-16  发布在  SQL Server
关注(0)|答案(1)|浏览(156)

It seems that Select Rand(5) always returns the same number - I checked on three computers. But can I assume that it will always (on any SQL Server instance) return the same number (0.713666525097956)?

And of course, if that will be the case for any other parameter of Rand() function.

kxeu7u2r

kxeu7u2r1#

This is documented and expected behaviour:
Returns a pseudo-randomfloat value from 0 through 1, exclusive.

seed

Is an integer expression ( tinyint , smallint , or int ) that gives the seed value. If seed isn't specified, the SQL Server Database Engine assigns a seed value at random. For a specified seed value, the result returned is always the same.

Emphasis mine.

If you want a true random number RAND is not your friend.

相关问题