Fun with RAND()

  • Comments posted to this topic are about the item Fun with RAND()

  • Great question, thanks.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Smart question :-):-)

    thank you Dave

    rabih

    rkaram

  • I hate myself not to read the relation correctly! 😀

  • Very good question. Thanks.

    I almost got it wrong but I realized in time that the loop had a "<" and not "<="

    ---------------
    Mel. 😎

  • SqlMel (11/5/2014)


    Very good question. Thanks.

    I almost got it wrong but I realized in time that the loop had a "<" and not "<="

    That's what got me. Need more coffee.

  • I think the answer is wrong, you can only have 1 row, but you can execute the select 0 to 9 times returning a single row.

  • Nice question, thank you.

  • alvertorave (11/5/2014)


    I think the answer is wrong, you can only have 1 row, but you can execute the select 0 to 9 times returning a single row.

    my thought exactly. i got it right, but ...

  • I forgot RAND() was exclusive. So we can get .99999..., which multiplies to 9.999999..... I take it then that storing a floating point number as an integer just truncates in SQL?

  • Nice question, Dave, and a good explanation. Thanks!

  • Ed Wagner (11/5/2014)


    SqlMel (11/5/2014)


    Very good question. Thanks.

    I almost got it wrong but I realized in time that the loop had a "<" and not "<="

    That's what got me. Need more coffee.

    Almost got me too. Thanks for the question.



    Everything is awesome!

  • Fun questions, thank you.

    Be still, and know that I am God - Psalm 46:10

  • Interesting Question, I ran it a couple of time to see the different results...

  • Brian.Klinect (11/5/2014)


    I forgot RAND() was exclusive. So we can get .99999..., which multiplies to 9.999999..... I take it then that storing a floating point number as an integer just truncates in SQL?

    This may be one of those "it depends" SQL Server answers.

    Run this code and see if your results are the same as mine.

    Declare @decFloat Float = 9.99999999999999;

    Declare @intTest Integer = @decFloat;

    Select @decFloat As [Float], @intTest As [Integer];

    -- Returns: 9.999999999999999

    Set @decFloat = 9.9999999999999999999999999;

    Set @intTest = @decFloat;

    Select @decFloat As [Float], @intTest As [Integer];

    -- Returns: 109

    Set @decFloat = 9.999999999999999;

    Set @intTest = @decFloat;

    Select @decFloat As [Float], @intTest As [Integer];

    -- Returns: 1010It appears that sometimes we get rounding and sometimes truncating.

    Enjoy!

Viewing 15 posts - 1 through 15 (of 27 total)

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