表中的每一行都需要一个不同的随机数。下面的代码对每一行使用相同的随机值。

SELECT table_name, RAND() magic_number 
FROM information_schema.tables 

我想从中得到一个INT或FLOAT。接下来,我将使用这个随机数从已知日期创建一个随机日期偏移量,例如从开始日期的1-14天偏移量。

这是为Microsoft SQL Server 2000。


当前回答

如果你需要保存你的种子,以便它每次生成“相同”的随机数据,你可以做以下事情:

1. 创建一个返回select rand()的视图

if object_id('cr_sample_randView') is not null
begin
    drop view cr_sample_randView
end
go

create view cr_sample_randView
as
select rand() as random_number
go

2. 创建一个从视图中选择值的UDF。

if object_id('cr_sample_fnPerRowRand') is not null
begin
    drop function cr_sample_fnPerRowRand
end
go

create function cr_sample_fnPerRowRand()
returns float
as
begin
    declare @returnValue float
    select @returnValue = random_number from cr_sample_randView
    return @returnValue
end
go

3.在选择数据之前,输入rand()函数,然后在选择语句中使用UDF。

select rand(200);   -- see the rand() function
with cte(id) as
(select row_number() over(order by object_id) from sys.all_objects)
select 
    id,
    dbo.cr_sample_fnPerRowRand()
from cte
where id <= 1000    -- limit the results to 1000 random numbers

其他回答

使用newid ()

select newid()

或者可能是这个

select binary_checksum(newid())

我有时对所选“答案”的问题是,分布并不总是均匀的。如果您需要在大量行中随机分布1 - 14,您可以这样做(我的数据库有511个表,所以这是可行的。如果你的行数比你的随机数跨度少,这就行不通了):

SELECT table_name, ntile(14) over(order by newId()) randomNumber 
FROM information_schema.tables

这种方法与普通随机解相反因为它保持了数字的顺序并使另一列随机化。

请记住,我的数据库中有511个表(这只与我们从information_schema中选择的b/c相关)。如果我把前面的查询放到临时表#X中,然后对结果数据运行这个查询:

select randomNumber, count(*) ct from #X
group by randomNumber

我得到这个结果,显示我的随机数非常均匀地分布在许多行中:

看看SQL Server -设置基于随机数有一个非常详细的解释。

综上所述,下面的代码生成了一个0到13之间的随机数,包括均匀分布:

ABS(CHECKSUM(NewId())) % 14

要更改范围,只需更改表达式末尾的数字。如果你需要一个同时包含正数和负数的范围,要格外小心。如果你做错了,可能会重复计算数字0。

A small warning for the math nuts in the room: there is a very slight bias in this code. CHECKSUM() results in numbers that are uniform across the entire range of the sql Int datatype, or at least as near so as my (the editor) testing can show. However, there will be some bias when CHECKSUM() produces a number at the very top end of that range. Any time you get a number between the maximum possible integer and the last exact multiple of the size of your desired range (14 in this case) before that maximum integer, those results are favored over the remaining portion of your range that cannot be produced from that last multiple of 14.

例如,假设Int类型的整个范围只有19。19是最大的整数。当CHECKSUM()的结果为14-19时,这些结果对应于结果0-5。这些数字比6-13更受欢迎,因为CHECKSUM()生成它们的可能性是前者的两倍。更容易直观地演示这一点。下面是我们的虚整数范围的全部可能结果集:

Checksum Integer: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Range Result:     0 1 2 3 4 5 6 7 8 9 10 11 12 13  0  1  2  3  4  5

你可以在这里看到,产生某些数字的机会比产生其他数字的机会更多:偏见。幸运的是,Int类型的实际范围要大得多……在大多数情况下,这种偏差几乎是无法检测到的。但是,如果您发现自己正在为严肃的安全代码执行此操作,则需要注意这一点。

回答老问题,但是这个答案之前没有提供,希望这对通过搜索引擎找到这个结果的人有用。

在SQL Server 2008中,引入了一个新函数CRYPT_GEN_RANDOM(8),它使用CryptoAPI生成一个加密强随机数,返回为VARBINARY(8000)。这里是文档页面:https://learn.microsoft.com/en-us/sql/t-sql/functions/crypt-gen-random-transact-sql

因此,为了获得一个随机数,你可以简单地调用函数并将其强制转换为必要的类型:

select CAST(CRYPT_GEN_RANDOM(8) AS bigint)

或者要获得一个介于-1和+1之间的浮点数,你可以这样做:

select CAST(CRYPT_GEN_RANDOM(8) AS bigint) % 1000000000 / 1000000000.0
select ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) as [Randomizer]

一直对我有效吗