Technical Article

Procedure for Random Numbers

,

Here is handy procedure to generate random number.
I find it useful when I generate large amount of test data in a database.
For example if you need to create a Customer Data and what they have purchased in the last month to run perfrmance tests on large amount of data an set up necessary indexes.
You can insert records into customer table and put in a random amount for the purchase price or assign a ID of related item id, if Index in the related table is sequential you can put in min(id) and Max(id) get a random item id and Assign it to your customer record.
Also you can use it when ever you need to generate a random number between 2 numbers.

Good Luck
Dan

CREATE PROCEDURE RandomNum(
@Num1 int,
@Num2 int
) 
AS

DECLARE @i int

IF @Num1 >= @Num2
RETURN
WHILE 1 = 1
BEGIN
SET @i = (SELECT rand() * @Num2 )
IF @i > @Num1
BEGIN
RETURN @i
BREAK
END
END

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating