Results 1 to 6 of 6

Thread: Ever use RAND() successfully??

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Ever use RAND() successfully??

    I want to randomly order a recordset of students from a student demographic table.

    Then I want to randomly order those same students in another query in the exact same random order

    RAND() stinks - nothing like VB RND...

    Code:
    SELECT RAND(),* from sometable
    gives a column of random values that are all the same - RAND() must be evaluated prior to the SELECT and the value populated throughout the recordset. What a useless function...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Ever use RAND() successfully??

    You have to use your imagination my friend

    This works:

    Code:
    create view RandView 
    as select rand() as RandomValue
    go 
    
    
    CREATE FUNCTION dbo.ReturnRand() 
    RETURNS real 
    AS 
    BEGIN 
     declare @RandomValue real 
     set @RandomValue = (select RandomValue from RandView) 
     return @RandomValue 
    END 
    GO 
    
    select dbo.ReturnRand(), * from sometable
    GO

  3. #3
    Fanatic Member Blade's Avatar
    Join Date
    Jan 1999
    Location
    Stoke-on-Trent, UK
    Posts
    527

    Re: Ever use RAND() successfully??

    Quote Originally Posted by szlamany
    I want to randomly order a recordset of students from a student demographic table.
    One way to do that is by using the NewID() function.

    try this:
    Code:
    SELECT * FROM SomeTable ORDER BY NewID()

  4. #4

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Ever use RAND() successfully??

    Quote Originally Posted by kaffenils
    You have to use your imagination my friend

    This works:

    Code:
    create view RandView 
    as select rand() as RandomValue
    go 
    
    
    CREATE FUNCTION dbo.ReturnRand() 
    RETURNS real 
    AS 
    BEGIN 
     declare @RandomValue real 
     set @RandomValue = (select RandomValue from RandView) 
     return @RandomValue 
    END 
    GO 
    
    select dbo.ReturnRand(), * from sometable
    GO
    I'll give that a try when I get to work - I was headed in that direction last night, but declared the function as INT, which of course killed the floating point values of RAND(). I was noticing that the SELECT off that type of function was very slow, though...

    I'm also now considering (after sleeping on it!) creating a more "permanent" table of student ids - put into a random order, and then simply using this table for queries during the scheduling process, which can take several weeks to complete. Any new students can simply be inserted into this "random order" table if the id is found to be missing.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Ever use RAND() successfully??

    In order to get it to work - it was required to seed the RAND() with a constant before the query on the student table...

    Code:
    select rand(100)
    select dbo.ReturnRand(), * from student_t order by 1
    GO
    Each run of this query gives the same consistent random order.

    Thanks again.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Ever use RAND() successfully??

    Quote Originally Posted by Blade
    One way to do that is by using the NewID() function.

    try this:
    Code:
    SELECT * FROM SomeTable ORDER BY NewID()
    Unfortunately that gives a different random order of students with each run of the query.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width