Hi ! I am in a need of generating Unique Random Numbers between given Number Range. For Example, a user provides a Number Range between 1 and 50. So, I need to generate a series of Unique Numbers from 1 to 50 in RANDOM ORDER. How can I do the same in MS-SQL? Kindly guide me.
If the range is 1 to 50, there isn't a lot of randomness to the number. Please do a search in Books Online for "random".
Thanks for reply. But, I need to generate a series of Unique Numbers from 1 to 50 in RANDOM ORDER. How can I do the same in MS-SQL? Kindly guide me.
A series of unique number is certainly different from a random number. One way could be to SELECT TOP (n) number FROM a number table ORDER BY NEWID() WHERE number BETWEEN lower_bound AND upper_bound;
declare @from_no int, @to_no int select @from_no =1, @to_no=50select num from( select row_number() over (order by s1.name) as num from sysobjects s1 cross join sysobjects s2) as twhere num>=@from_no and num<=@to_no order by newid()