[英]Generate 5 random digit number using Javascript in Adobe Acrobat
I'm using if (this.getField("Form No").valueAsString=="") {
this.getField("Form No").value = util.printf("%05d", Math.floor((Math.random() * 99999) + 1000));
}我正在使用
if (this.getField("Form No").valueAsString=="") {
this.getField("Form No").value = util.printf("%05d", Math.floor((Math.random() * 99999) + 1000));
}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. 尝试在打开时用PDF中的5位随机数字填充文本字段。 It seems to generate a number ranging from 5-6 digits instead of exactly 5.
它似乎会产生5到6位数而不是5的数字。
What am I doing wrong? 我究竟做错了什么?
Thanks for the reples. 感谢您的宝贵意见。 I'm sorry but I'm very inexperienced when it comes to javascript.
抱歉,关于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? 但是我实际上如何将其放在自己的代码中,最小为10000,最大为99999?
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
). 表达
rnd() * 99999
会给你范围内的数0..99999
(这里所有范围都在下端包容性和排他性的上部,所以这个范围不包括99999
)。 When you add 1000
to that you'll get a number in the range 1000..100999
. 当您添加
1000
时,您将得到一个范围为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. 那是四位数,五位数或六位数,尽管您永远不会看到四位数,因为您用
%05d
强制前导零打印它们。 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. 但是,
%05d
指定了最小字段宽度,因此将使六位数的数字不变,因此为什么会看到五位数和六位数的数字。
If you're after a five digit number without leading zeroes, you should instead be getting a number in the range 0..90000 then adding ten thousand to translate that to
10000..100000`, something like: 如果您使用的是五位数后没有前导零的数字,则应该获取0..90000范围内的数字,
then adding ten thousand to translate that to
10000..100000`,例如:
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: 例如,它也可以轻松地从
0..100000
使用五位数字进行0..100000
:
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
). 如果那是您想要的(您的问题有点含糊,因为它既增加了偏移量,就像您希望它从
10000
开始, 又使用%05d
,就好像您允许的数字小于 10000
)。
So, bottom line, if you're after numbers in 10000..100000
and `0..100000 (as string padded to five characters), use respectively: 因此,最重要的是,如果您要输入
10000..100000
和`0..100000(作为填充为五个字符的字符串)中的数字,请分别使用:
strVal = util.printf("%5d",getRangeVal(10000, 99999));
strVal = util.printf("%05d",getRangeVal(0, 99999));
If you need to generate a number in the range 0 to 99,999 with 5 digits, you'll need to preserve leading zeros. 如果您需要生成一个0到99,999范围内的5位数字,则需要保留前导零。
Consider using toFixed to retain leading zeros (provided a string is OK): 考虑使用toFixed保留前导零(只要字符串可以):
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. 但是,如果您希望数字在10,000到99,9999之间,则paxdiablo可以解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.