Ridam Jindal
Ridam Jindal

Reputation: 67

Is there a possibility of generating a 6 digit unique random number every time with xdmp:random() or some other way in Marklogic

I tried using xdmp:random(999999) but it sometimes generates a 5 digit value and sometimes a 6 digit value - Is there any way of getting the every time unique 6 digit value?

Upvotes: 1

Views: 153

Answers (2)

hunterhacker
hunterhacker

Reputation: 7132

If leading zeroes don’t work, make sure your base value is 100,000 and go up to 999,999.

xdmp:random(899999) + 100000

Upvotes: 2

grtjn
grtjn

Reputation: 20414

Simplest is to pad with zeros using fn:format-number:

format-number(xdmp:random(999999), '000000')

Alternatively, you could also look at sem:uuid-string, which gives better random results with a fixed string-length.

HTH!

Upvotes: 4

Related Questions