Skip to main content
Participant
March 22, 2017
Question

Creating Random Dice Numbers in PDFs

  • March 22, 2017
  • 1 reply
  • 327 views

Hi!

I do a lot of work for tabletop gaming companies and I'm hoping to develop a feature in InDesign where the exported PDF creates and displays a random number between say 1-20 inclusive. I was looking at setting this up in the InDesign file, but I've been directed to post this query here.

The random number would appear as though it was on the face of one side of a dice, and would be different every time you open the document.

I've had a bit of help from a JS savvy mate, and are using the following coding:

function d20() {

  return Math.floor(Math.random() * 21);

}

the trick I'm facing is associating this function with a particular text box. I've added it to the JS Document Scripts, just how do I get it to display in the document?

Thanks heaps for your help.

This topic has been closed for replies.

1 reply

Inspiring
March 22, 2017

That function will return a number from 0 to 20, not 1 to 20.

You could add the following code to a document-level JavaScript to set the field to a number from 1 to 20 when the document is opened:

getField("Text1").value = util.printd("%.0f", 1 + Math.floor(Math.random() * 20));

where Text1 is the name of the text field. Exactly how you add a document-level JavaScript depends on which version of Acrobat you're using.