Skip to main content
berts74981696
Participant
February 1, 2018
Answered

Generating a Random number

  • February 1, 2018
  • 1 reply
  • 655 views

I am completely new at javascript and I don't know all the terminology so it might take some dumbing down in response. I am attempting to generate a random number between 0 and 1 so I would like it to look like .1234 or .9999. It has to be four digits and I don't necessarily need it to lock once entered. I would only need to print that page and hit my radio button and generate a new list of numbers. I currently have this as my script.

this.getField("Text1").value =

Math.floor(Math.random()/Math.pow(10000,4));

The "Text1" is the current name for the box on my test page and I am using a radio button to generate the number. I will also have multiple boxes that I want random numbers generated in and I would like to use one radio button to complete the task of generating all the random numbers that I would need. How would I also add, say "text2, text3, text4", boxes to this so that all of them would have a separate random number or do I have to enter a script to each "text" box to do this.

Thanks 

This topic has been closed for replies.
Correct answer Thom Parker

JavaScript Random numbers are already between 0 and 1, so all you need is some formatting.

this.getField("Text1").value = Math.random().toString().substr(0,6);

1 reply

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
February 1, 2018

JavaScript Random numbers are already between 0 and 1, so all you need is some formatting.

this.getField("Text1").value = Math.random().toString().substr(0,6);

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often