Skip to content

? Roll, Deal, Permute

Random lists, with or without duplicates

Roll and Deal

Select items randomly, generate random values

    x?y     ?[x;y]          / Roll
neg[x]?y    ?[neg[x];y]     / Deal



  • x is an integer atom
  • y is a list

returns abs[x] randomly selected items of y. Where x is

  • positive items are selected independently (Roll)
  • negative and x>=neg count y, items are selected from different indexes of y (Deal)
Duplicate items in y

If y contains duplicate items, so may the result of Deal.




  • x is an int atom
  • y is an atom > 0

returns a list of abs[x] items of the same type as y, generated as follows

right domain (y)     range                            operator
integer >0           til y                            Roll, Deal
0Ng                  GUIDs                            Roll, Deal
float, temporal ≥0   0 to y                           Roll
0i                   ints                             Roll
0                    longs                            Roll, Deal
0b                   01b                              Roll
" "                  .Q.a                             Roll
0x0                  bytes                            Roll
numeric symbol `n    symbols, each of n chars (n≤8)   Roll, Deal
                     from abcdefghijklmnop

Where x is negative (Deal), y must have a positive long or null GUID

q)10?5                                        / roll 10 (5-sided dice)
4 2 1 1 3 2 0 0 2 2
q)-5?20                                       / deal 5
13 11 8 12 19
q)-10?10                                      / first 10 ints in random order
9 3 5 7 2 0 6 1 4 8
q)(asc -10?10)~asc -10?10

q)-1?0Ng                                      / deal 1 GUID
q)count distinct -1000?0Ng                    / deal 1000 GUIDs

q)5?4.5                                       / roll floats
3.13239 1.699364 2.898484 1.334554 3.085937

q)4?2012.09m                                  / roll months
2006.02 2007.07 2007.07 2008.06m

q)30?" "

q)16?0x0                                      / roll 16 bytes

q)20?0b                                       / roll booleans

q)10?`3                                       / roll short symbols
q)rand `6

Roll and Deal return list results

For an atom result, instead of first 1?x, use rand.

Deal of GUID atom

Deal of GUID uses a mix of process ID, current time and IP address to generate the GUID, and successive calls may not allow enough time for the current time reading to change.

q)count distinct {-1?0ng}each til 10  / Deal one GUID ten times

The range of GUIDs is large enough that Roll and Deal often return the same result.

q)count distinct 1000000000?0Ng  / Roll a billion GUIDs

For a set of distinct GUIDs, use Deal to generate them in one operation



Where x is

  • a non-negative int atom, returns the items of til x in random order
  • a list, returns the items of x in random order

(Since V3.3.)

q)0N?10                         / permute til 10
8 2 4 1 6 0 5 3 7 9
q)0N?5 4 2                      / permute items
4 5 2
q)0N?"abc"                      / permute items
q)0N?("the";1 2 4;`ibm`goog)    / permute items
1 2 4


Deal, Roll, Permute and rand use a constant seed on kdb+ startup: scripts using them can be repeated with the same results. You can see and set the value of the seed with system command \S.)

To use GUIDs as identifiers, use Deal, not Roll

$ q
q)1?0Ng                                    / roll 1 GUID
$ q
q)1?0Ng                                    / roll 1 GUID
$ q
q)-1?0Ng                                   / deal 1 GUID
$ q
q)-1?0Ng                                   / deal 1 GUID

Roll uses the current seed (\S 0N). Deal uses a seed based on process properties and the current time. This means -10?0Ng is different from {first -1?0Ng}each til 10.


error cause
length neg x exceeds count y
type x is negative (Roll only)

system command \S
command-line option -S