1

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?

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • Are six-digit numbers with a leading zero acceptable? – Michael Kay Jul 26 '20 at 20:19
  • No leading zero is not acceptable as per the requirement.Thanks but this worked - format-number(xdmp:random(999999), '000000'). – Ridam Jindal Jul 26 '20 at 20:39
  • I asked because that call on format-number() will sometimes produce a number with a leading zero. – Michael Kay Jul 27 '20 at 11:31
  • You're concerned about getting unique values but wanting to use random. I think those requirements conflict. What are you trying to accomplish? – Dave Cassel Jul 27 '20 at 14:19
  • @MichaelKay - Ok I thought you meant that padding every random number with leading zeroes in case the count of/length of random number does not matches the required count of 6. – Ridam Jindal Jul 27 '20 at 20:54
  • @DaveCassel - I am basically working to figure out that in the application where I am involved will be generating an OTP on every new login of any user.So I tried making it unique with xdmp:random() but since the resultant value is quite large as per user's perspective so I wanted to precise that every unique random value to a length of 6. – Ridam Jindal Jul 27 '20 at 20:56
  • Okay. You should expect to get collisions sometimes and plan for that. The only way to avoid that is to use a UUID (sem:uuid()). Even if you add a couple more digits, it might still happen, so your application should keep that in mind. – Dave Cassel Jul 28 '20 at 21:42

2 Answers2

4

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!

grtjn
  • 20,254
  • 1
  • 24
  • 35
  • Thanks much!! The above worked well .I hope this will not return any duplicate entries. – Ridam Jindal Jul 26 '20 at 20:38
  • You said in comments that a six-digit number containing a leading zero is not acceptable: ie. you're concerned with the range being 100000 to 999999, not with the format being six digits. If that's the case then this solution looks wrong. – Michael Kay Jul 27 '20 at 11:33
  • That comment came after posting this answer, but if it is true, use hunterhackers solution instead. – grtjn Jul 27 '20 at 13:06
  • Note though that collisions (e.g. duplicates) cannot be prevented this way, not even with sem:uuid-string, however unlikely (and with sem:uuid, changes really are rare). I created this ml-unique library a while ago, which compares a few alternatives: https://github.com/grtjn/ml-unique#how-it-works – grtjn Jul 27 '20 at 13:09
  • @grtjn - Thanks for sharing this ml-unique library!!Yes will approach the hunterhackers solution . – Ridam Jindal Jul 27 '20 at 21:04
2

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

xdmp:random(899999) + 100000

hunterhacker
  • 6,378
  • 1
  • 14
  • 11