Hi all,I was sent the following code that generates random numbers for the UK lottery but I can't work out whether it allows duplicates (the variable concerned is @allowdups)Also, is there a more efficient way of doing this?Anyway, here it is:DECLARE @allowdups BIT = 0DECLARE @uppermain INT = 50DECLARE @lowermain INT = 1DECLARE @upperbon INT = 11DECLARE @lowerbon INT = 1DECLARE @maincount INT = 5DECLARE @bonuscount INT = 2 CREATE TABLE #MainNumberBoundaries(Number TINYINT,UpperLimit DECIMAL (15, 10))CREATE TABLE #BonusNumberBoundaries(Number TINYINT,UpperLimit DECIMAL (15, 10))INSERT #MainNumberBoundaries (Number, UpperLimit)SELECT sv.Number, (1.0000000/(@uppermain + 1 - @lowermain) * (sv.Number + 1 - @lowermain)) AS UpperLimitFROM master..spt_values svWHERE sv.Number <= @uppermainAND sv.Number >= @lowermainAND sv.type = 'P'INSERT #BonusNumberBoundaries (Number, UpperLimit)SELECT sv.Number, (1.0000000/(@upperbon + 1 - @lowerbon) * (sv.Number + 1 - @lowerbon)) AS UpperLimitFROM master..spt_values svWHERE sv.Number <= @upperbonAND sv.Number >= @lowerbonAND sv.type = 'P'CREATE TABLE #MainNumbersToOutput(Number TINYINT NOT NULL)WHILE (SELECT COUNT(*) FROM #MainNumbersToOutput) < @maincountBEGINWITH MyNumber (Number)AS (SELECT TOP 1 nb.NumberFROM #MainNumberBoundaries nbWHERE RAND() < nb.UpperLimitORDER BY nb.Number ASC)INSERT #MainNumbersToOutputSELECT mn.NumberFROM MyNumber mnWHERE @allowdups = 1OR mn.Number NOT IN (SELECT Number FROM #MainNumbersToOutput)ENDCREATE TABLE #BonusNumbersToOutput(Number TINYINT NOT NULL)WHILE (SELECT COUNT(*) FROM #BonusNumbersToOutput) < @bonuscountBEGINWITH MyNumber (Number)AS (SELECT TOP 1 nb.NumberFROM #BonusNumberBoundaries nbWHERE RAND() < nb.UpperLimitORDER BY nb.Number ASC)INSERT #BonusNumbersToOutputSELECT mn.NumberFROM MyNumber mnWHERE @allowdups = 1OR mn.Number NOT IN (SELECT Number FROM #BonusNumbersToOutput)ENDSELECT Number, 'M'FROM #MainNumbersToOutputUNION ALLSELECT Number, 'B'FROM #BonusNumbersToOutputDROP TABLE #MainNumberBoundariesDROP TABLE #BonusNumberBoundariesDROP TABLE #MainNumbersToOutputDROP TABLE #BonusNumbersToOutput