I want to randomly choose a value from a small range of integers (less than 200). As an alternative to
SELECT RAND()
I'm trying to use
CAST(CAST(CRYPT_GEN_RANDOM(2) AS INTEGER) AS FLOAT) / 65535
but I'm getting some strange effects.
For example:
WITH Numbers (num)
AS
(
SELECT num
FROM (
VALUES (1), (2), (3), (4),
(5), (6), (7), (8),
(9), (10)
) AS Numbers (num)
),
RandomNumber (num)
AS
(
SELECT CAST(
(CAST(CAST(CRYPT_GEN_RANDOM(2) AS INTEGER) AS FLOAT) / 65535)
* (SELECT COUNT(*) FROM Numbers) + 1
AS INTEGER
)
)
SELECT T1.num, R1.num
FROM Numbers AS T1
INNER JOIN RandomNumber AS R1
ON T1.num = R1.num;
I'd expect this to return exactly one row with both column values equal.
However, it returns zero, one or more rows, with the column values being only occasionally equal.
Any idea what's going on here?