Random Number Generating

  • LOL.. He also wants "random" and "unique"... Given that any randomly generated number always has the potential to collide with another randomly generated number, the base requirements are a contradiction right out of the gate.

    Considering that contradiction the best we can do is give the appearance of "random" and "unique" to the casual observer. Varying the length is "doable" but would only create headaches down the road.

    BUT... Just because we shouldn't, doesn't mean we can't!  XD

    WITH 
    cte_n1 (n) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) n (n)),-- 10
    cte_n2 (n) AS (SELECT 1 FROM cte_n1 a CROSS JOIN cte_n1 b),-- 100
    cte_n3 (n) AS (SELECT 1 FROM cte_n2 a CROSS JOIN cte_n2 b),-- 10,000
    cte_Tally (n) AS (
    SELECT TOP(10000000)-- (10M rows to be generated)
    ROW_NUMBER() OVER (ORDER BY a.n)
    FROM
    cte_n3 a CROSS JOIN cte_n3 b-- 100,000,000
    ORDER BY
    a.n
    )
    SELECT
    n = ROW_NUMBER() OVER (ORDER BY NEWID() ),
    rand_num = t.n + lv.len_val
    FROM
    cte_Tally t
    CROSS APPLY ( VALUES (11 * CONVERT(bigint, POWER(10.0, ABS(CHECKSUM(NEWID())) % 5 + 8))) ) lv (len_val)
    GO

    2023-03-02 17_35_00-Window

  • I just wanna know what he actually wants. 😀

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden wrote:

    I just wanna know what he actually wants. 😀

    Ditto!

Viewing 3 posts - 16 through 17 (of 17 total)

You must be logged in to reply to this topic. Login to reply