-- create table to hold the stickers create table #stickers(number int, numfound int); create index #pk_stickers on #stickers(number) -- create table to hold the total sticker count create table #stickercount(number int, numfound int); create index #pk_stickercount on #stickercount(number) -- create table to hold the number needed for a complete set create table #numberneeded(numfound int) declare @counter int = 1, @stickersneeded int = 0, @stickernumber int -- load the sticker and sticker count tables with the 64 stickers while @Counter <= 64 begin insert into #stickers values(@counter, 0) insert into #stickercount values(@counter, 0) select @counter = @counter + 1 end -- loop through 240,000 stickers select @counter = 1 while @counter <= 240000 begin -- get a random sticker number select @stickernumber = convert(int, rand() * 64) + 1 -- add it to the stickers and the sticker count tables update #stickers set numfound = numfound + 1 where number = @stickernumber update #stickercount set numfound = numfound + 1 where number = @stickernumber -- if a complete set has been found if (select min(numfound) from #stickers) > 0 begin -- Add the number of stickers needed to the number needed table insert into #numberneeded values(@counter - @stickersneeded) -- update the counter so we'll know how many are needed next time select @stickersneeded = @counter -- clear the stickers table update #stickers set numfound = 0 end -- go to the next sticker select @counter = @counter + 1 end select * from #numberneeded select avg(numfound) from #numberneeded select * from #stickercount select sum(numfound) from #stickercount select min(numfound) from #stickercount select max(numfound) from #stickercount drop table #numberneeded drop table #stickercount drop table #stickers