471,350 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Need help with SP


I was asked to create a SP that will return 7 random numbers from a range of
numbers. SP should take 3 parameters: MinValue, MaxValue and InitialSeed.
If and only if all 3 parameters are the same, the same 7 random numbers
should return.

I have done something so far, just don't know if this is the right way to do
it. Please take a look and I appreciate any suggestions.

Alter Procedure dbo.spRandom7
@MinValue int
, @MaxValue int
, @InitSeed bigint
Declare @CurrValue int
Declare @MaxLoop int
Declare @CurrLoop int
Declare @SeedDelta float
Declare @CurrSeed float
Declare @Selected int
Create Table #SelectedNumbers
(Value int)

Set @MaxLoop = 100
Set @CurrLoop = 1

Set @CurrSeed = Cast( @InitSeed As Float)
Set @SeedDelta = Cast ((@InitSeed / @MaxLoop) As Float)

Set @Selected = 0

While (@MaxLoop >= @CurrLoop And @Selected < 7)
Set @CurrValue = @MinValue + Round( (@MaxValue - @MinValue) *
Rand(@CurrSeed), 0)

If Not Exists(Select * From #SelectedNumbers Where Value = @CurrValue)
Insert Into #SelectedNumbers Values(@CurrValue)
Set @Selected = @Selected + 1

Set @CurrSeed = Cast( (@InitSeed - @CurrLoop * @SeedDelta) As Float)

Set @CurrLoop = @CurrLoop + 1


Select * From #SelectedNumbers


Feb 7 '06 #1
0 823

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Sofia | last post: by
15 posts views Thread by drdoubt | last post: by
4 posts views Thread by Phil | last post: by
2 posts views Thread by Michael R. Pierotti | last post: by
reply views Thread by U S Contractors Offering Service A Non-profit | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.