Expand my Community achievements bar.

Generating a Random Number for application Form

Avatar

Former Community Member
I have an application form that people fill out and submit for consideration. After they open the doc I want it to generate a random number for a confirmation code that they can include in the memo field when they mail in a check to be approved. I think I just need a number field with an initiate calculation. Any help would be great!
8 Replies

Avatar

Former Community Member
You can deploy a script that generates random numbers and letters for a unique identifier, something like the following:



function RandomGenerator()

{

var RandomLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890";

var sequence = "";

var FieldFillSize = 10;

for (x = 1; x <= FieldFillSize; x++) {



index = Math.floor(Math.random() * RandomLetters.length);

sequence = sequence + RandomLetters.charAt(index);

}



return sequence;

}

Avatar

Former Community Member
I'm not familiar with using scripts in this version. Can you explain a little more please?

Avatar

Former Community Member
You can use the Math.random() function to generate a random number between 0 and 1, and then write your own function to turn that into a confirmation code.



For example, in the initialize event for a field you could use code like this:



--

var confirmCode = "";

for (var i=0; i<8; i++)

{



confirmCode += String.fromCharCode(65 + (Math.random() * 26.0));



}



this.rawValue = confirmCode;

--



To put in this code, drop a field on the form, select the field, and then show the Script Editor window. In the Show list in the Script Editor, pick the Initialize event, set the Language to JavaScript, and paste in the code above.

--

SteveX

Adobe Systems

Avatar

Former Community Member
that great, but how do I create a number between 0 and 1000000. I'm no programmer, help please.

Avatar

Former Community Member
Try Math.random() * 1000000



Chris

Adobe Enterprise Developer Support

Avatar

Former Community Member
Math.random() generates a number between 0 and 1, so just multiply it by 1000000:



this.rawValue = Math.random() * 1000000;



That gives you a fractional value; to round it to the nearest whole number, use something like this:



this.rawValue = Math.round(Math.random() * 1000000);



(This code goes into the initialize event; make sure that you've set the Language to JavaScript).



--

SteveX

Adobe Systems

Avatar

Former Community Member
Thanks for your help guys, I'm not a programmer at all period. I was able to generate a randon by using:



var randomnumber = Math.floor(Math.random()*99999);

NumericField1.rawValue = randomnumber;



Next, how about a obtain a random number, r, between two user-defined integer values, m and n, and send the result to anumeric field?



Define r as r=m+Math.round(Math.random()*n) and do this



var x=document.myform;

var m=x.m.value;

var n=x.n.value;

var r=m+Math.round(Math.random()*n)

x.r.value=r;



Would the above work? If I was working with numbers 1-5000 where would I insert these values? Thanks again for all your help. I tired using a XML table and importing data butthat was a joke.