S.V
S.V

Reputation: 2793

KDB: generate random string

How can one generate a random string of a given length in KDB? The string should be composed of both upper and lower case alphabet characters as well as digits, and the first character can not be a digit.

Example:

"i0J2Jx3qa" / OK
"30J2Jx3qa" / bad

Thank you very much for your help!

Upvotes: 0

Views: 650

Answers (4)

Darren
Darren

Reputation: 549

There's already a fine answer above which has been accepted. Just wanted somewhere to note that if this is to generate truly random data you need to consider randomising your seed. This can be done in Linux by using $RANDOM in bash or reading up to four bytes from /dev/random (relatively recent versions of kdb can read directly from FIFOs).

Otherwise the seed is set to digits from pi: 314159

Upvotes: 0

SJT
SJT

Reputation: 1097

If you prefer without the repetitions:

raze(1,stringLength-1)?'10 0_\:.Q.nA,.Q.a

Upvotes: 1

Matt Moore
Matt Moore

Reputation: 2775

For the purposes of creating random data you can also use ?/deal with a number of characters up to 8 as a symbol(which you could string). This doesn't include numbers though so just an alternative approach to your own answer.

1?`8
,`bghgobnj

Upvotes: 0

S.V
S.V

Reputation: 2793

stringLength: 13
randomString: (1 ? .Q.A,.Q.a) , ((stringLength-1) ? .Q.nA,.Q.a)

Upvotes: 3

Related Questions