Random number !

  • I have a function which generate random number in range :

    CREATE FUNCTION Func_Rand

    (

    @MAX BIGINT ,

    @UNIQID UNIQUEIDENTIFIER

    )

    RETURNS BIGINT

    AS

    BEGIN

    RETURN (ABS(CHECKSUM(@UNIQID)) % @MAX)+1

    END

    GO

    If you run this script you always get result between 1 and 4

    SELECT dbo.Func_Rand(4, NEWID())

    BUT, in this script sometimes have no result !!!!!!! why??

    SELECT 'aa'

    WHERE dbo.Func_Rand(4, NEWID()) IN ( 1, 2, 3, 4 )

  • farax_x (10/3/2015)


    I have a function which generate random number in range :

    CREATE FUNCTION Func_Rand

    (

    @MAX BIGINT ,

    @UNIQID UNIQUEIDENTIFIER

    )

    RETURNS BIGINT

    AS

    BEGIN

    RETURN (ABS(CHECKSUM(@UNIQID)) % @MAX)+1

    END

    GO

    If you run this script you always get result between 1 and 4

    SELECT dbo.Func_Rand(4, NEWID())

    BUT, in this script sometimes have no result !!!!!!! why??

    SELECT 'aa'

    WHERE dbo.Func_Rand(4, NEWID()) IN ( 1, 2, 3, 4 )

    Don't rely on implicit cast, convert the output of the function to an integer if you are comparing it to an integer.

    😎

  • what should I do for comparing ?

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply