# Random Number Generator Script – SQL Query

- Link: https://techaid24.com/random-number-generator-script/
- Published: 2026-01-16T04:34:25+06:00
- Author: Selim Ahmed

In this article, we&#039;ll take a look at Show 1) Method 1: Generate Random Numbers (Int) between Rang 2) Method 2: Generate Random Float Numbers 3) Method 3: Random Numbers Quick Scripts 4) Method 4: Random Numbers (Float, Int) Tables Based on Time 5) Method 5: Random number on a per-row basis In this article, we want to discuss a nice function to random number generator script in SQL Server. Before we get started, if you want to know about split with any delimited string, please go through the following article: Update table from another table in SQL Server. There are many methods to generate random numbers in SQL Server. Let’s start. Method 1: 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 123456789101112 &nbsp;---- Create the variables for the random number generationDECLARE @Random INT;DECLARE @Upper INT;DECLARE @Lower INT&nbsp;---- This will create a random number between 1 and 999SET @Lower = 1 ---- The lowest random numberSET @Upper = 999 ---- The highest random numberSELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)SELECT @Random&nbsp; Method 2: Generate Random Float Numbers SELECT RAND( (DATEPART(mm, GETDATE()) * 100000 ) + (DATEPART(ss, GETDATE()) * 1000 ) + DATEPART(ms, GETDATE()) ) 12345 &nbsp;SELECT RAND( (DATEPART(mm, GETDATE()) * 100000 )+ (DATEPART(ss, GETDATE()) * 1000 )+ DATEPART(ms, GETDATE()) )&nbsp; Method 3: Random Numbers Quick Scripts ---- Random float from 0 up to 20 - [0, 20) SELECT 20*RAND() ---- Random float from 10 up to 30 - [10, 30) SELECT 10 + (30-10)*RAND() ---- Random integer BETWEEN 0 AND 20 - [0, 20] SELECT CONVERT(INT, (20+1)*RAND()) ---- Random integer BETWEEN 10 AND 30 - [10, 30] SELECT 10 + CONVERT(INT, (30-10+1)*RAND()) 12345678910 &nbsp;---- Random float from 0 up to 20 - [0, 20)SELECT 20*RAND()---- Random float from 10 up to 30 - [10, 30)SELECT 10 + (30-10)*RAND()---- Random integer BETWEEN 0 AND 20 - [0, 20]SELECT CONVERT(INT, (20+1)*RAND())---- Random integer BETWEEN 10 AND 30 - [10, 30]SELECT 10 + CONVERT(INT, (30-10+1)*RAND())&nbsp; Method 4: Random Numbers (Float, Int) Tables Based on Time DECLARE @t TABLE( randnum float ) DECLARE @cnt INT; SET @cnt = 0 WHILE @cnt &lt;=10000 BEGIN SET @cnt = @cnt + 1 INSERT INTO @t SELECT RAND( (DATEPART(mm, GETDATE()) * 100000 ) + (DATEPART(ss, GETDATE()) * 1000 ) + DATEPART(ms, GETDATE()) ) END SELECT randnum, COUNT(*) FROM @t GROUP BY randnum 123456789101112131415 &nbsp;DECLARE @t TABLE( randnum float )DECLARE @cnt INT; SET @cnt = 0WHILE @cnt &lt;=10000BEGINSET @cnt = @cnt + 1INSERT INTO @tSELECT RAND( (DATEPART(mm, GETDATE()) * 100000 )+ (DATEPART(ss, GETDATE()) * 1000 )+ DATEPART(ms, GETDATE()) )ENDSELECT randnum, COUNT(*)FROM @tGROUP BY randnum&nbsp; Method 5: Random number on a per-row basis ---- The distribution is pretty good however there are the occasional peaks. ---- If you want to change the range of values just change the 1000 to the maximum value you want. ---- Use this as the source of a report server report and chart the results to see the distribution SELECT randomNumber, COUNT(1) countOfRandomNumber FROM ( SELECT ABS(CAST(NEWID() AS binary(6)) %1000) + 1 randomNumber FROM sysobjects) sample GROUP BY randomNumber ORDER BY randomNumber 1234567891011 &nbsp;---- The distribution is pretty good however there are the occasional peaks.---- If you want to change the range of values just change the 1000 to the maximum value you want.---- Use this as the source of a report server report and chart the results to see the distributionSELECT randomNumber, COUNT(1) countOfRandomNumberFROM (SELECT ABS(CAST(NEWID() AS binary(6)) %1000) + 1 randomNumberFROM sysobjects) sampleGROUP BY randomNumberORDER BY randomNumber&nbsp; Random Number Generator Script – SQL Query
