Expression rand(checksum(newid()))
is often used to generate random numbers.
While generating some test data, I executed following statement:
select rand(checksum(newid())) R1, rand(checksum(newid())) R2
from ftSequence(3)
where ftSequence(N)
is a table function returning single column N
and values 1, 2, 3 ... N
in its rows (as much as argument N
is). Running this resulted to quite expected data:
R1 R2
---------------------- ----------------------
0,817 0,9515
0,3043 0,3947
0,5336 0,7963
Then it was necessary to find sum in each column, and I did:
select sum(rand(checksum(newid()))) S1, sum(rand(checksum(newid()))) S2
from ftSequence(3)
Surprisingly I got the same number in each column:
S1 S2
---------------------- ----------------------
1,2276 1,2276
Why it happens?
The same behavior for avg
, min
and max
aggregate functions.
Is it query optimizer, or do I miss some logic?
More observations after comments.
Placing sum(rand(checksum(newid())))
into CTE or subquery like
select
(select sum(rand(checksum(newid()))) from ftSequence(3)) S1,
(select sum(rand(checksum(newid()))) from ftSequence(3)) S2
or
select sum(R1) S1, sum(R2) S2
from (
select rand(checksum(newid())) R1, rand(checksum(newid())) R2
from ftSequence(3)
) R
as well as doing a trick like
select
sum(rand(checksum(newid()))) S1
, sum(rand(checksum(newid())) + 0) S2
from ftSequence(3)
worked, resulting to different values
S1 S2
---------------------- ----------------------
0,7349 1,478
Happy of that, and needed to produce more than one row of several different avg(rand(checksum(newid()))) from ftSequence(3)
, I did following
select R.*
from ftSequence(3) S1
cross join (
select
avg(rand(checksum(newid()))) R1,
avg(rand(checksum(newid())) + 0) R2
from ftSequence(3)
) R
and got following result:
R1 R2
---------------------- ----------------------
0,6464 0,4501
0,6464 0,4501
0,6464 0,4501
At this point I was not able to answer myself, whether is it correct result, or should values be all random? What are the ways to make all values random?