Sample code for 30+ languages & platforms
SQL Server

Generating Random Integer in Range

See more PRNG Examples

Demonstrates how to generate random integers in a specified range.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @success int
    SELECT @success = 0

    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    SELECT @success = 0

    DECLARE @fortuna int
    EXEC @hr = sp_OACreate 'Chilkat.Prng', @fortuna OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Generate random integers between 12 and 24 inclusive
    DECLARE @i int

    SELECT @i = 0
    WHILE @i <= 100
      BEGIN
        EXEC sp_OAMethod @fortuna, 'RandomInt', @iTmp0 OUT, 12, 24
        PRINT @iTmp0
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @fortuna


END
GO