NewID() and randomization?

  • We have a program that's supposed to return a random set of rows from a rowset of 500-600 records.  The original programmer had used the NewID() function to achieve randomization.   It appeared to work fine, but in the past few months, it now seems to be picking up the same records each time.

    The only thing that has changed is the database.  It was on a Windows Server 2000 machine running SQL Server 2000.  The database now lives on a Windows Server 2003 machine running SQL Server 2000.

    This makes me wonder - is NewID() truly random?   What does NewID() seed from?

    Is there a best practice method for achieving truly random results?

    Thanks in advance for any insight or suggestions.

  • NewID() seeds from a combination of the network card identification number (unique) and a number derived from the CPU clock. While there's no such thing as "truly random" in computing, it's pretty close and it's bizarre that you see repeating values. I'd assume you're using a SELECT TOP x ... ORDER BY NewID() approach. With that it's theoretically possible for the same x rows to be ranked in the same order repeatedly even if NewID() ends up actually being random.

    I can't begin to posit an explanation however.

  • Thanks for letting me know what it's seeding from.  The query does show SELECT TOP x ... ORDER  BY NewID()

    I've gone back to the user to get examples of what they're seeing with the randomization not working, as looking at the data, I don't see the problem.

     

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

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