#1.
SELECT abs(CAST(CRYPT_GEN_RANDOM(4) AS INT))
#2.
CREATE FUNCTION dbo.MyRAND(@Seed as bigint) RETURNS float(53) AS
BEGIN
--Sample: SELECT dbo.MyRAND(DEFAULT), dbo.MyRAND(DEFAULT), dbo.MyRAND(12345) FROM ( SELECT 1 AS ID UNION SELECT 2 UNION SELECT 3 ) as ThreeRows
DECLARE @Return as float(53)
IF @Seed = 0
SET @Return = (Cast(CRYPT_GEN_RANDOM(8) as bigint) + POWER(Cast(2 as float(53)), 63)) / POWER(2.0, 64)
ELSE
SET @Return = (Cast(CRYPT_GEN_RANDOM(8, CAST(@Seed AS varbinary(8))) as bigint) + POWER(Cast(2 as float(53)), 63)) / POWER(2.0, 64)
RETURN @Return
END
참고(original article) : http://stackoverflow.com/questions/8378587/why-is-rand-not-producing-random-numbers
반응형