If you just need unique, fairly-random-looking numbers, and you don't have to work around any existing ones, the quickest way by far would be to use a simple linear congruential generator in an update.Say you want to create 10000 rows:CREATE TABLE cards (i int IDENTITY PRIMARY KEY, cardnum int NOT NULL)-- here, numbers is just any old table with at least 10000 rows!INSERT INTO cards SELECT TOP 10000 0 FROM numbersDECLARE @n int-- seed the generatorSET @n = FLOOR(RAND() * 10000)-- set the cardnum and generate the next valueUPDATE cards SET cardnum = @n, @n = (@n*3421+1) % 10000
Knuth (as reported by my trusty copy of Sedgewick) says that ideally, the number that is 3421 in this instance should have one digit less than the modulus, should end in "21" and the next digit should be even.You'll probably want to make @n a bigint (SQL Server 2000 only) if you're generating values much larger than 10000.Edited by - Arnold Fribble on 02/28/2002 15:33:05