I Need to generate a random number between two numbers for each row.
For Eg:
select ColName, (Random nos between 1500 to 2000) from TableName
Thanks in advance
Random number formula is
RANDOM DECIMAL RANGE
SELECT RAND()*(b-a)+a;
Example
SELECT RAND()*(25-10)+10; // Random between 10 to 25
RANDOM INTEGER RANGE
SELECT FLOOR(RAND()*(b-a)+a);
Example
SELECT FLOOR(RAND()*(25-10)+10);
DECLARE @start INT
DECLARE @end INT
SET @start = 1500
SET @end = 2000
SELECT Round(( ( @end - @start - 1 ) * Rand() + @start ), 0)
Update : To get random number for each row.
select ABS(Checksum(NewID()) % (@end-@start)) + @start
a little change required, by replacing the following code it will respond.
Round(( ( @end - @start - 1 ) * Rand(checksum(newid())) + @start ), 0)
Eg:
Generate Same Value :
select top 10 ROUND(((2000 - 1500 -1) * RAND() + 1500), 0)
from sysobjects
Generate Different Value :
select top 10 ROUND(((2000 - 1500 -1) * Rand(checksum(newid()))+ 1500), 0)
from sysobjects
SELECT ROUND(((2000 - 1500 -1) * RAND() + 1500), 0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.