我有以下内容,它将在数据库中返回 100k 行,这应该会找到我 6 个随机值。问题在于乐透中不能有多个相同的值。我注意到有些值显示相同,例如..
21-21-34-3-14-46
很明显,21
并且21
不是乐透彩票中的有效数字。所以我的问题是如何检查生成随机值时没有相同的数字?
这是我的代码:
SET NOCOUNT ON
DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <=500000)
BEGIN
--Do Stuff
DECLARE @Random1 INT, @Random2 INT, @Random3 INT, @Random4 INT, @Random5 INT, @Random6 INT, @Upper INT, @Lower INT
---- This will create a random number between 1 and 49
SET @Lower = 1 ---- The lowest random number
SET @Upper = 49 ---- The highest random number
SELECT @Random1 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random2 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random3 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random4 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random5 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random6 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
--SELECT @Random;
INSERT INTO CustomerSelections
([draw_date]
,[val1]
,[val2]
,[val3]
,[val4]
,[val5]
,[val6])
VALUES
(
'2013-07-05'
,@Random1
,@Random2
,@Random3
,@Random4
,@Random5
,@Random6
)
SET @intFlag = @intFlag + 1
END
GO
@戈登: