INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Microsoft SQL Server: Programming FAQ

## Random Numbers

 How Can I Generate a Series of Random Integers With T-SQL? by tlbroadbent faq183-1142 Posted: 3 Oct 01 Here is a stored procedure that will generate a series of random numbers and return them in a result set./*This SQL Server stored procedure returns a result setcontaining a series of random integersInputs are:  @begin_no - lowest number to be generated,  @end_no - highest number to be generatedCalling syntax:   Exec sp_GenerateRandomNumberResultSet Begin, EndCreated by Terry L. Broadbent, Oct 3 2001Please inform me of any bugs encountered. Comments and suggestions for improvement are encouraged.*/CREATE procedure sp_GenerateRandomNumberResultSet  @begin_no int=1, @end_no int=100 AsSet nocount onDeclare @rn int, @rec int, @cnt intSet @cnt=@end_no-@begin_no+1Create Table #t (RecNo int identity(1,1), RandNo int null)Set @rec=1While @rec<=@cnt Begin  Insert Into #t (RandNo) Values (null)  Set @rec=@rec+1 EndSet @rec=1While @rec<=@cnt Begin  Set @rn=rand()*@cnt+@begin_no  If @begin_no<0 Set @rn=@rn-1  Update #t Set RandNo=@rn  Where RecNo=@rec    And @rn Not In      (Select RandNo      From #t Where RandNo Is Not Null)  If @@rowcount>0 Set @rec=@rec+1 EndSet nocount offSelect RandNo From #tDrop Table #tGo Back to Microsoft SQL Server: Programming FAQ Index Back to Microsoft SQL Server: Programming Forum

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!