IT Community - Software Programming, Web Development and Technical Support

How to Generate Various types of random numbers using SQLServer ?

This is a discussion on How to Generate Various types of random numbers using SQLServer ? within the Database Support forums, part of the Web Development category; To Generate Random Numbers (Int) between Rang: -- Create the variables for the random number generation DECLARE @Random int; DECLARE @Upper ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Database Support

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-16-2007, 12:13 AM
Archer Archer is offline
D-Web Programmer
 
Join Date: Jun 2007
Posts: 52
Archer is on a distinguished road
Default How to Generate Various types of random numbers using SQLServer ?

To Generate Random Numbers (Int) between Rang:
-- Create the variables for the random number generation
DECLARE @Random int;
DECLARE @Upper int;
DECLARE @Lower int

-- This will create a random number between 1 and 999
SET @Lower = 1 -- The lowest random number
SET @Upper = 999 -- The highest random number
SELECT @Random = Round(((@Upper - @Lower -1) * Rand() + @Lower), 0)
SELECT @Random


To Generate Random Float Numbers:
SELECT RAND( (DATEPART(mm, GETDATE()) * 100000 )
+ (DATEPART(ss, GETDATE()) * 1000 )
+ DATEPART(ms, GETDATE()) )
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-16-2007, 02:26 AM
Gopisoft Gopisoft is offline
D-Web Sr.Programmer
 
Join Date: Feb 2007
Posts: 117
Gopisoft is on a distinguished road
Default Re: How to Generate Various types of random numbers using SQLServer ?

Hi,

Generate Random Numbers

SQL Server has a built-in function that generates a random number, the RAND() mathematical function.The RAND math function returns a random float value from 0 through 1.

SELECT RAND() AS [RandomNumber]

RandomNumber
---------------------
0.34344339282376501



The output of the RAND function will always be a value between 0 and 1.
If you want to generate a random integer number, all you have to do is multiply it by the maximum value you want generated and then get rid of the decimal places. One way of getting rid of the decimal places is by CASTing it to INT. Here's an example of generating a random number with a maximum value of 999,999:


SELECT CAST(RAND() * 1000000 AS INT) AS [RandomNumber]


RandomNumber
------------
163819




SELECT TOP 10 RAND() AS [RandomNumber], [CustomerID], [CompanyName], [ContactName]
FROM [dbo].[Customers]


RandomNumber CustomerID
-------------------- ----------
0.21090395019612362 AL001
0.21090395019612362 AN001
0.21090395019612362 AN003
0.21090395019612362 AR005
0.21090395019612362 BE067
0.21090395019612362 AUS07
0.21090395019612362 BL076
0.21090395019612362 BO405
0.21090395019612362 BA909
0.21090395019612362 BM809



"Note: Repetitive invocations of RAND() in a single query will produce the same value."

The NEWID() Way

he different way of generating a random number without using the RAND mathematical function. The NEWID system function can be used to generate a random numeric value as can be seen from the following SELECT statement.

SELECT ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RandomNumber]

RandomNumber
------------
403663676


The NEWID system function returns a unique value of uniqueidentifier data type. order to convert this to an integer data type, it first has to be converted to VARBINARY then it can be converted to integer, as can be seen by the two CAST statements. The resulting integer value can be positive and negative.

SELECT TOP 10 ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) AS [RandomNumber],
[CustomerID], [CompanyName], [ContactName]
FROM [dbo].[Customers]



RandomNumber CustomerID
-------------------- ----------
1120919216 AL001
1227765350 AN001
677704826 AN003
151612960 AR005
755868329 BE067
1566550720 AUS07
1511953511 BL076
1280157238 BO405
1283976604 BA909
989554291 BM809



-R.Gopi
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I generate a random number from php? itbarota PHP Programming 2 09-25-2007 08:57 AM
MultiLanguageSupport-SQLSERVER Murali Database Support 2 07-28-2007 03:28 AM
How to generate random password generator.using javascript.? itbarota HTML, CSS and Javascript Coding Techniques 1 07-26-2007 12:42 AM
How many types of authentication modes are there and what are the Types.... Archer ASP and ASP.NET Programming 1 07-25-2007 03:48 AM
How do I generate a random number from php? kingmaker PHP Programming 2 07-24-2007 02:56 AM


All times are GMT -7. The time now is 03:45 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0