What sort of transaction isolation are you using? In a multi-threaded environment, what happens when you have two threads with the same value that both pass the first part of the test, but then baulk at the second? Secondly, do you really think this is a good idea? I don't. It really won't scale as you get close to the maximum number of possible values as your generator routine will mostly generate already used values. If you want to do it this way, I think it would be better to have a table of all possible values, have another field that indicates whether the field has been used or not, and use a SELECT TOP 1 RandomNumber FROM RandomNumbers WHERE HasBeenUsed = 0 ORDER BY NewID() But do you really need random IDs? It seems you just need IDs that haven't been used before. Could I suggest you use GUIDs instead? You can either set a field to a GUID type, or set it to varchar/char and use NewID() to generate the value and insert it. Cheers Ken : -----Original Message----- : From: thelist-bounces at lists.evolt.org [mailto:thelist- : bounces at lists.evolt.org] On Behalf Of Brian Cummiskey : Sent: Friday, 17 March 2006 8:45 AM : To: thelist at lists.evolt.org : Subject: Re: [thelist] math help... : : Joshua Olson wrote: : : > Assuming that the random is truly random, you should have (26 + 10)^5 : > permutations, or 60466176. : : Thanks Josh. : : Time to tear apart this stored procedure... looks like it's all taking : place here: : : : if exists( : select confirmation_code : from TABLE : where confirmation_code=@confirmation_code : ) : ---the confirmation code has been used : begin : goto repeat : -- re-does the whole random process : end : else : ---get the cconfirmation code successfully : begin : insert TABLE (confirmation_code) : values (@confirmation_code) : end : : : : Looks right to me :( : Anyone see anything?