This procedure generates random unique numbers between two numbers. It gets 3 parameters. The first parameter is number you want to list, the second parameter is the start value and the last parameter is the end value.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Create procedure randomGenerate(@unit int,@min int,@max int) as Begin Declare @numbers table(number int) Declare @i int = 0 Declare @number int while (@i<@unit) Begin Set @number = floor(rand()*(@max-@min+1))+@min if(not exists(Select * from @numbers where number = @number)) begin insert into @numbers values(@number) Set @i = @i + 1 end end Select * from @numbers order by 1 End |

After you run the code above than you run the code below.

1 |
Execute randomGenerate 5,20,30 |

**Result**

Lists 5 numbers between 20 and 30

1 |
Execute randomGenerate 6,0,49 |

Result

Lists 6 numbers between 0 and 49

1 |
Execute randomGenerate 10,50,100 |

Result

Lists 10 numbers between 50and 100