Need help with SP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Danny Ni

    #1

    Need help with SP

    Hi,

    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
    )
    AS
    BEGIN
    Declare @CurrValue int
    Declare @MaxLoop int
    Declare @CurrLoop int
    Declare @SeedDelta float
    Declare @CurrSeed float
    Declare @Selected int


    Create Table #SelectedNumber s
    (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)
    Begin
    Set @CurrValue = @MinValue + Round( (@MaxValue - @MinValue) *
    Rand(@CurrSeed) , 0)

    If Not Exists(Select * From #SelectedNumber s Where Value = @CurrValue)
    Begin
    Insert Into #SelectedNumber s Values(@CurrVal ue)
    Set @Selected = @Selected + 1
    End

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

    Set @CurrLoop = @CurrLoop + 1

    End

    Select * From #SelectedNumber s

    END




Working...