Results 1 to 8 of 8

Thread: Generate a Random number between 10,000 and 99,999

  1. #1

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Generate a Random number between 10,000 and 99,999

    Hi all,

    Just wondering if anyone had an idea on how to generate a random number between the range of 10,000 and 99,999 (Sql Server 2000). I think I can make use of Rand() but I don't think that this would be a different number everytime time it was called and not sure how to give it min of 10,000 and max of 99,999. I would assume that this is pretty common and so there must be something built in?

    Any ideas?

  2. #2
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Generate a Random number between 10,000 and 99,999

    You can try

    VB Code:
    1. Dim myVal As long
    2.   Randomize
    3.   Do While MyVal < 10000
    4.     MyVal = Rnd * 100000
    5.   Loop
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Generate a Random number between 10,000 and 99,999

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim MyNumber As Long
    3. Randomize
    4. MyNumber = (99999 * Rnd) + 10000
    5. Text1.Text = MyNumber
    6. End Sub

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Generate a Random number between 10,000 and 99,999

    If you are looking for a T-SQL solution - here's some info...

    We like to get the same random order from one run to the next - so we seed the RAND() function at the start of our STORED PROCEDURE...

    DECLARE @RD REAL
    SET @RD=RAND(117)

    We chose 117 for no particular reason - but it's a steady consistent seed, so that the random order from that point forward will be consistent. I'm guessing that if you do not seed it will be a different random order from one run to the next - but you will have to check that.

    Then, in a LOOP in the SPROC we keep calling...

    SET @RD=RAND()

    Each time we call for RAND() we get a different value.

    BOOKS ONLINE says that RAND() will return a value from 0 through 1 - but I'm guessing it either never returns 0 or never returns 1 - one or the other.

    So with that said...

    SELECT (@RD * 89999) + 10000

    Should do what you want.

    Big problems with RAND() though - if you use it in an UPDATE statement for lots of records, they all get the same RAND().

    If you use it in INSERT INTO .... SELECT ... RAND() ...

    they all get the same RAND().

    You kind of have to do the work in a table variable in the stored procedure - in a loop - that's worked well for us.

    Good luck!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Re: Generate a Random number between 10,000 and 99,999

    For the T-SQL solution, we wanted to put this into a function so we can use it all over as needed, but apparently you cannot call Rand() in a function (UDF) because it is non-deterministic. I am curious as to how you implemented it?

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Generate a Random number between 10,000 and 99,999

    Quote Originally Posted by VBCrazyCoder
    For the T-SQL solution, we wanted to put this into a function so we can use it all over as needed, but apparently you cannot call Rand() in a function (UDF) because it is non-deterministic. I am curious as to how you implemented it?
    Yep - you are right - cannot use it in a FUNCTION - we went down that path as well!

    It was quite a fight...

    We used it in a SPROC - here's a snippet from the stored procedure.

    VB Code:
    1. Set @RD=Rand(117)
    2. Set @RI=(Select Min(StuId) From Algorithm_T Where Yr=@CrsYr and Bldg=@Bldg and RandomOrder is null)
    3.  
    4. While @RI is not Null
    5. Begin
    6.     Update Algorithm_T Set RandomOrder=Rand() Where Yr=@CrsYr and Bldg=@Bldg and StuId=@RI
    7.     Set @RI=(Select Min(StuId) From Algorithm_T Where Yr=@CrsYr and Bldg=@Bldg and RandomOrder is null)
    8. End

    Basically we decided to actually STORE the random figure in a table - since we kept getting burned with all these restrictions on the RAND() function.

    We created a table - put a REAL column in it for the value.

    And you can see we are looping through the table filling in the RANDOMORDER column - if it's NULL we fill it in, and then get the next kid.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Generate a Random number between 10,000 and 99,999

    BTW - there are tricks to getting around the non-deterministic problem.

    Put it in a VIEW - have the FUNCTION look at the VIEW??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Re: Generate a Random number between 10,000 and 99,999

    I went the function / view route - works great! Thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width