Random Number Generator in SQL Server?

  • Is there a random number generator at all in SQL Server?  Can it be used in a stored procedure?  Or will i have to do random number with the program and pass them to SQL Server?

  • This will give you what you need (think I got it from here originaly) delete or add a line to get more or less digits:

    select CAST ( CONVERT ( INT, 10*rand()) AS CHAR(1))

    +CAST ( CONVERT ( INT, 10*rand()) AS CHAR(1))

    +CAST ( CONVERT ( INT, 10*rand()) AS CHAR(1))

    +CAST ( CONVERT ( INT, 10*rand()) AS CHAR(1))

    +CAST ( CONVERT ( INT, 10*rand()) AS CHAR(1))

    +CAST ( CONVERT ( INT, 10*rand()) AS CHAR(1))

    +CAST ( CONVERT ( INT, 10*rand()) AS CHAR(1))

    +CAST ( CONVERT ( INT, 10*rand()) AS CHAR(1))

     

    Don't realy know how well it will perform resource wise though.

  • Research Using RAND and NEWID in BOL.



    Good Hunting!

    AJ Ahrens


    webmaster@kritter.net

  • thanks guys!

  • I have always used newid() for a random selection of records something like:

    select top 100 name from names

    order by newid()

     

    Paul.

  • Paul's approach can be used highly effectively if you have a table full of integers. Just use Top 1 and the filter criteria, and you can use it whereever you need a random number.

    NewID() is very handy when trying to generate test data I find.

     

  • A couple of thoughts.

    Depending on your application, these may or may not be suitable. For example they would not be good for a high security app.

    Another thing to remember is that many random number generators do not repeat (or repeat only after the whole cycle is completed) whereas a real world random number can repeat. (NewID by definition will not repeat). If you are using it to test an application where repetitions of data are possible, make sure repeats do occasionally occur (truncating a random number will often produce occasional repeats).

    Finally notice that rand(seed) has aother odd behavior, unlike normal random number generators, similar seeds start at similar 'random' values.

    ...

    -- FORTRAN manual for Xerox Computers --

  • Regarding the rand(seed) option, something that gives you better randomization is to run the results through a reverse(rand(seed)) operation. rand(1), rand(2), rand(3) sorted asc will return results in the same order as 1,2,3. By running through the reverse function you get a much more random response.

    Also, don't forget that ordering by any function will not allow the use of an index so ordering on a resultset with N records, although appearing relatively fast for a powerful machine, will not scale.

  • Hi I am a noob to sql and am creating a random number generator on sql 2005.  I got some code from a old proc but dont understand one portion.

    SET @Lower = 1000 -- The lowest random number

    SET @Upper = 9999 -- The highest random number

    SELECT @random = Round(((@Upper - @Lower - 1) * Rand() + @Lower), 0)

    I need a 4 digit number generated but what is the -1 for?

    Thanks,

  • SELECT @random = 1000 + ABS(CHECKSUM(NEWID())) % 9000

     


    N 56°04'39.16"
    E 12°55'05.25"

  • And, Peter's fine method works not only for single variables, but for a whole table, as well.

    SELECT (ABS(CHECKSUM(NEWID())) % 10000) AS RandomNumber

    FROM Master.dbo.Syscolumns

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

Viewing 11 posts - 1 through 10 (of 10 total)

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