Search code examples
randomkdb+

Making random numbers (roll) that are different each time you run q


I'd like to use roll e.g.

1?6 

to generate numbers that are different each time I restart q.

The fixed seed normally prevents this, e.g. every time I quit q, restart, and enter 1?6, I get 4.

I am imagining a complex solution but is there an easy, built-in solution you can think of?

Thanks so much.

Edit: I did try using deal (e.g. -1?6) which I thought according to the documentation should work, but it still gives the same number every startup. Any idea why?


Solution

  • I misread your question initially - to make the random generator different each time you have to set a different seed number, see https://code.kx.com/q/basics/syscmds/#s-random-seed

    Something like

    >q
    KDB+ 4.0 2021.02.02 Copyright (C) 1993-2021 Kx Systems
    w32/ 4()core 4095MB tlynch ctx-tlynch01 10.203.90.98 NONEXPIRE
    
    q)system"S ",string"j"$.z.t
    q)1?100
    ,73
    q)\\
    
    >q
    KDB+ 4.0 2021.02.02 Copyright (C) 1993-2021 Kx Systems
    w32/ 4()core 4095MB tlynch ctx-tlynch01 10.203.90.98 NONEXPIRE
    
    q)system"S ",string"j"$.z.t
    q)1?100
    ,52
    

    The difference between roll and deal (position/negative) is that in deal the same value can't appear twice. No big reason to use neg rather than minus sign other than it makes it more obvious/explicit to the reader that it's a deal