在某一个区间内生成随机数
原理,rand函数会生成一个在0到1之间的随机float数。
用这个随机数*区间范围+最小值,就得到一个区间范围内的随机数。然后用round函数去掉小数点后面的。
DECLARE @Random INT;DECLARE @Upper INT;DECLARE @Lower INTSET @Lower = 3 ---- The lowest random numberSET @Upper = 7 ---- One more than the highest random numberSELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)SELECT @Random
生成布尔值
If you are only generating one row, you could use something as simple as:
SELECT CAST(ROUND(RAND(),0) AS BIT)
However, if you are generating more than one row, RAND()
will evaluate to the same value for every row, so please see .
也可以是这样
SELECT ROUND(RAND(),0)