0

I'm using if (this.getField("Form No").valueAsString=="") {
this.getField("Form No").value = util.printf("%05d", Math.floor((Math.random() * 99999) + 1000));
}

In an attempt to fill a text field with a random 5 digit number in a PDF upon open. It seems to generate a number ranging from 5-6 digits instead of exactly 5.

What am I doing wrong?

Thanks for the reples. I'm sorry but I'm very inexperienced when it comes to javascript. I assume you mean:

function getRandomArbitrary(min, max) { return Math.random() * (max - min) + min; }

But how would I actually put this in my own code with the 10000 as min and 99999 as max?

  • 2
    You're adding 1000 – Jeremy Jackson Mar 01 '17 at 01:30
  • Possible duplicate of [Generating random whole numbers in JavaScript in a specific range?](http://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range) – 4castle Mar 01 '17 at 01:30
  • Use the algorithm in duplicate question, with `10000` as the minimum, and `99999` as the maximum. If you're okay with leading zeros, use `0` as the minimum. – 4castle Mar 01 '17 at 01:31
  • Thanks for the reply. I'm sorry but I'm very inexperienced when it comes to javascript. I assume you mean: function getRandomArbitrary(min, max) { return Math.random() * (max - min) + min; } But how would I actually put this in my own code with the 10000 as min and 99999 as max? – christo_morgo Mar 01 '17 at 01:37
  • *"But how would I actually put this in my own code"* - Just define that `getRandomArbitrary()` function and call it with those values: `getRandomArbitrary(10000, 99999)`. – nnnnnn Mar 01 '17 at 01:52

2 Answers2

1

The expression rnd() * 99999 will give you a number in the range 0..99999 (all ranges here are inclusive at the lower end and exclusive at the upper, so this range does not include 99999). When you add 1000 to that you'll get a number in the range 1000..100999.

That's four-, five-, or six-digits, though you'll never see four-digit ones since you print them with %05d forcing a leading zero. However, the %05d specifies a minimum field width so will pass six-digit numbers through unchanged hence why you see five- and six-digit ones.

If you're after a five digit number without leading zeroes, you should instead be getting a number in the range 0..90000then adding ten thousand to translate that to10000..100000`, something like:

Math.floor(Math.random() * 90000) + 10000

Or, using a generalised function:

function getRangeVal(lo, hi) {
    return Math.floor(Math.random() * (hi + 1 - lo)) + lo;
}
val = getRangeVal(10000, 99999);

This latter method has the advantage of being able to generate numbers from an arbitrary range without having to think too much. For example, it will also easily do a five-digit number from 0..100000 with:

val = getRangeVal(0, 100000);

if that's what you were after (your question is a little vague on that, since it both adds an offset as if you wanted it to start at 10000 and uses %05d as if you were allowing for numbers below 10000).

So, bottom line, if you're after numbers in 10000..100000 and `0..100000 (as string padded to five characters), use respectively:

strVal = util.printf("%5d",getRangeVal(10000, 99999));
strVal = util.printf("%05d",getRangeVal(0, 99999));
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Thanks mate, so using: 'Math.floor(Math.random() * 90000) + 10000' should give me the desired result of a random 5 digit number? – christo_morgo Mar 01 '17 at 02:01
  • @christo, yes, assuming your definition of "5 digit number" is the range `10000` through `99999` inclusive at both ends. However, I'd still advise using the `getRangeVal()` function so you never have to think about this again. Life's too short to be writing the same code over and over throughout your career :-) – paxdiablo Mar 01 '17 at 02:02
  • Thank you. 90% of what you said doesn't make much sense to me, as my experience with JS is near zero. I'm not sure why I would use the `getRangeVal()` if the ` 'Math.floor(Math.random() * 90000) + 10000' `works for what I'm wanting. I'm not even sure how I would add: `strVal = util.printf("%5d",getRangeVal(10000, 99999)); strVal = util.printf("%05d",getRangeVal(0, 99999));` To my code anyway.. I'm a total noob, but really appreciate your help! – christo_morgo Mar 01 '17 at 02:09
  • That's okay, @christo, just use the 'bottom line' stuff at the end. In terms of how you use the `util.printf`, use it as you currently do in your question, assigning it to the `form number` field value. But, if you'd prefer to use the simple calculation rather than the function, that's fine. It will work just as well in this case. – paxdiablo Mar 01 '17 at 02:16
0

If you need to generate a number in the range 0 to 99,999 with 5 digits, you'll need to preserve leading zeros.

Consider using toFixed to retain leading zeros (provided a string is OK):

console.log( Math.random().toFixed(5).replace(/\d\./,'') );

You could also use a function to pad with a specified number of leading zeros, but that seems a bit more complex than is required.

However, if you want numbers in the range 10,000 to 99,9999 paxdiablo has the answer.

RobG
  • 142,382
  • 31
  • 172
  • 209