6

我有一些使用以下技术生成随机数的 SQL 代码:

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


with nums as (
    select @lower as n
    union all
    select nums.n+1
    from nums
    where nums.n < @Upper
   ),
   randnums as 
   (select nums.n, ROW_NUMBER() over (order by newid()) as seqnum
    from nums
   )
select @Random1 = MAX(case when rn.seqnum = 1 then rn.n end),
       @Random2 = MAX(case when rn.seqnum = 2 then rn.n end),
       @Random3 = MAX(case when rn.seqnum = 3 then rn.n end),
       @Random4 = MAX(case when rn.seqnum = 4 then rn.n end),
       @Random5 = MAX(case when rn.seqnum = 5 then rn.n end),
       @Random6 = MAX(case when rn.seqnum = 6 then rn.n end)
from randnums rn;

select @Random1, @Random2, @Random3, @Random4, @Random5, @Random6

我的问题是这个数字生成有多随机?还有另一种更“随机”的方法吗?

我在用:

Microsoft SQL Server 2008 (SP3) - 10.0.5512.0 (X64)   Aug 22 2012 19:25:47   Copyright (c) 1988-2008 Microsoft Corporation  Developer Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) 

大多数解决方案的问题是你最终会得到这样的值:14,29,8,14,27,27我不能有重复的数字!

4

4 回答 4

14

我想你可以做这样的事情更简单,更容易

DECLARE @Upper INT;
DECLARE @Lower INT;
SET @Lower = 1;     /* -- The lowest random number */
SET @Upper = 49;    /* -- The highest random number */
    
    
SELECT @Lower + CONVERT(INT, (@Upper-@Lower+1)*RAND());

为了获得一个不重复的随机数,这将完成这项工作

WITH CTE 
AS
(
    SELECT  randomNumber, COUNT(1) countOfRandomNumber
    FROM (
    SELECT ABS(CAST(NEWID() AS binary(6)) %49) + 1 randomNumber
    FROM sysobjects
    ) sample
    GROUP BY randomNumber
)
SELECT TOP 5 randomNumber
FROM CTE
ORDER BY newid() 

要设置最高限制,您可以将 49 替换为您的最高限制编号。

于 2013-11-09T12:30:12.877 回答
0

对于Laravel

 public function generatUniqueId()
    {
        $rand = rand(10000, 99999);
        $itemId = $rand;
        while (true) {
            if (!BookItem::whereBookItemId($itemId)->exists()) {
                break;
            }
            $itemId = rand(10000, 99999);
        }

        return $itemId;
    }
于 2019-06-25T12:42:08.137 回答
0
  1. 创建一个随机数列表。对于这个例子,我做了 100 个,可能更多,可能更少(但不低于你的限制)

  2. 使用 row_number() 函数检测重复项

  3. Once you delete the duplicates, select top 6 number in your list

     with RandomNumbers as
     (
         select id = 1, number = round(((56 - 1 -1) * RAND(CHECKSUM(NEWID())) + 1), 0),
         orderid = round(((56 - 1 -1) * RAND(CHECKSUM(NEWID())) + 1), 0)
         union all
         select id + 1, round(((56 - 1 -1) * RAND(CHECKSUM(NEWID())) + 1), 0), round(((56 - 1 -1) * RAND(CHECKSUM(NEWID())) + 1), 0) 
         from RandomNumbers where id < 100
     ), 
     NoDuplicates as
     (
         select number, id = row_number() over (partition by number orderid by order) from
         (
             select numeber, order, repeat = row_number() over (partition by numeber orderid by order)
             from RandomNumbers 
         ) a
         where repeat = 1
     )
    

    select * from NoDuplicates where id <= 6

于 2021-11-05T03:29:22.387 回答
-2

您可以使用 Rand() 函数。

select CEILING(RAND() *<max of random numbers))
于 2013-11-09T20:59:20.480 回答