These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Generate Random Number in a Numberic Field

vargs
Registered: Jul 2 2010
Posts: 26
Answered

Hello,

I am wondering if there is a way to Generate a random number in a numeric Field or any other type?

I am currently using LiveCycle Designer ES 8.2

thanks,

-Vargs

radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
A random number can be generated easily.

  1. // Returns a random number between 1 and 9999
  2. this.rawValue = Math.floor(Math.random() * 9999)

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You need to use JavaScirpt. The following code could be added to a button to display a random number when ever the button is clicked.
// Returns a random integer between min and max   // Using Math.round() will give you a non-uniform distribution!   function getRandomInt(min, max){return Math.floor(Math.random() * (max - min + 1)) + min;} var random = getRandomInt(1, 6);xfa.host.messageBox( "Random number: " + random );

Other variations for genterating a random number include:
// Returns a random number between 0 (inclusive) and 1 (exclusive)   function getRandom(){return Math.random();}

// Returns a random number between min and max   function getRandomArbitary(min, max){return Math.random() * (max - min) + min;}

George Kaiser

vargs
Registered: Jul 2 2010
Posts: 26
Thanks for the post, I was researching further into what I wanted to do and found this post

http://www.acrobatusers.com/forums/aucbb/viewtopic.php?pid=60135

What I want to do with this random number is generate it every time someone opens a new form. This form will be saved on a persons computer and worked on. So I don't want the number to change every time the file is open, only when the form doesn't have a unique number already in it.

I believe the answer is the following but I am not sure if it will do the task I want to perform. Here is the following code

Quote from the post by user Jonom:

"Here's a script I've used on the Initialize event of a form. It checks to see if it already has a value and if so doesn't generate a new number in case people save the form and reopen it:

The number is unique to the second, so two people would have to open the form at the same second to get the same value..."

****************************************************
Code:

if (this.rawValue == null) {
var d = new Date();
//divide result by 1000 to get seconds and round off
this.rawValue = parseInt(d / 1000);
}
else {
this.rawValue = rawValue;
}

end quote

****************************************************
How can I put in this code into a numeric field. I made a numeric field in Livecycle Designer ES 8.2
and tried to add in code but was not sure which event to put it in or where exactly it should be placed.


Thanks,

-Vargs
jonom
Registered: Jan 31 2008
Posts: 133
Hi Vargs, the code goes on the Initialize event of the field. I use a text field but numeric will be fine, you'll just need to set a display pattern to get rid of commas.

It's not a random number, the Date() function returns the number of milliseconds from the Epoch (January 1, 1970). I'm then dividing by 1000 to get seconds so it's a smaller number to deal with. One potential advantage of this is that given the number on the form you can extract the time and date.

I've also update the code slightly:

if (this.rawValue == null){var d = new Date();//divide by 1000 to get seconds and trim decimalsthis.rawValue = Math.floor(d / 1000);}else{this.rawValue = rawValue;}
vargs
Registered: Jul 2 2010
Posts: 26
Hello,

I used a Text Field but it would not let me access the Initialize state to insert in the code.

I then reverted back to using a numeric field and added in the code right below
"form1.#pages[0|.Page2.button1:: initialize - FormCalc, client)

I then tried to preview the pdf and recieved the following error:

"syntax error near token "{" on line 2 column 1.

thanks
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
All sample codes here are in JavaScript but you set the language to FormCalc, that's why you get an error.

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

vargs
Registered: Jul 2 2010
Posts: 26
Thank you so much Jonom for the marvelous code and radzmar for being so patient with me.
I didn't notice that change in language. I thought it was all the same language input.

thank you so much

-vargs
jonom
Registered: Jan 31 2008
Posts: 133
vargs wrote:
I used a Text Field but it would not let me access the Initialize state to insert in the code.
You were probably using a text box, not a text field (fillable object).