How do I set random numbers that fall in a range in kdb+?

时间:2019-02-23 22:41:11

标签: kdb

In Kdb+, how do I use the "roll" function to make the random numbers generated fall within a range that doesn't start with 0? For example what if I wanted the range to be within 2-10 instead of 0-10?

What do I have to add to the code to make it fall into a range instead of the default 0-x? I have tried and looked for every method but can't seem to find one.

4 个答案:

答案 0 :(得分:4)

您也可以从0-8滚动,然后加两个。不需要预先生成列表

select u, min(abs(TIMESTAMPDIFF(SECOND, d, '2018-12-07 23:00:00')))
from ...
group by u

假设您希望包含2-10个数字

答案 1 :(得分:0)

// quick and simple method
q)10?2+til 8
6 2 4 3 4 3 4 5 4 7
// or function (x)=num to be dealt, (y) start range, (z) end range
q)f:{x?y+til 1+z-y}
q)f[10;10;20]
12 17 10 11 19 12 11 18 18 11

答案 2 :(得分:0)

如果您在右侧参数中提供了一个列表,那么您将从该列表中获得一个随机值。要滚动2-10之间的随机范围,您可以使用til生成范围:

q)2+til 9
2 3 4 5 6 7 8 9 10
q)1?2+til 9
,6

您甚至可以提供常规列表以从中随机抽取:

q)3?(`abc;2 3f;10;20;30;"text")
2 3f
`abc
"text"

答案 3 :(得分:-1)

用于随机数生成器的简单数学函数是:

(rand()mod(1 + max- min))+分钟

q) f:{x+rand[0] mod  1+y-x}
q) f[5;10]
q) 7

更新:我没有注意到您想生成该范围内的几个随机数。您可以轻松地修改上述功能:

q) f:{x+(z?0) mod  1+y-x}
q) f[2;10;4]
q) 6 4 7 2