Skip to content

Instantly share code, notes, and snippets.

@hafuu
Created February 3, 2016 06:05
Show Gist options
  • Save hafuu/aaec73e72a02deb43e14 to your computer and use it in GitHub Desktop.
Save hafuu/aaec73e72a02deb43e14 to your computer and use it in GitHub Desktop.
ランダム in 再帰CTE
DECLARE @a AS bigint = 1103515245;
DECLARE @b AS bigint = 12345;
DECLARE @c AS bigint = 2147483647;
DECLARE @x AS bigint = (@a * 3 + @b) % @c;
DECLARE @y AS bigint = (@a * @x + @b) % @c;
WITH t AS(
SELECT
0 AS Id
, @x AS X
, @y AS Y
-- 次に使う乱数。カラム間の参照ができないので次のを事前に計算しておく。
, (@a * @y + @b) % @c AS NEXT_X
, (@a * ((@a * @y + @b) % @c) + @b) % @c AS NEXT_Y
UNION ALL
SELECT
Id + 1
, NEXT_X
, NEXT_Y
-- 次に使う乱数
, (@a * NEXT_Y + @b) % @c
, (@a * ((@a * NEXT_Y + @b) % @c) + @b) % @c
FROM
t
WHERE
Id < 100
)
SELECT * FROM t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment