Skip to main content
Known Participant
Nov 13, 2020 3:03 PM UTC
Answered

Random number

Hi, guys I really need help with this one, I know basic javascripting but am clueless how to get this.
I have button to which I wanna add script so every time I click it, it generates random number between 1-20 and show result in text field called "Result"
(That part I know how to do) BUT I would like every time it generate number, that number is excluded from next roll until all 20 numbers is rolled.

This topic is closed to new replies. Start a new post to keep the conversation going.
Thom Parker
Thom ParkerCorrect answer
Community Expert
Nov 14, 2020 9:07 AM UTC

Thank you Thom, it's working, but on every first click it shows "undefined" and after that it works.

Is it supposed to do that?


Yes, the first click is for initialization. My idea was that the initialiation would be moving to a different button, but here's a version that blends the init into the first number.

 

if(!this.pastNumbers)
    this.pastNumbers = [];

if(this.pastNumbers.length < 20)
{
   var randomNumber;
   do{
       randomNumber = Math.ceil(Math.random()*20);
   }while(this.pastNumbers.indexOf(randomNumber)!=-1);
   this.pastNumbers.push(randomNumber);
}
else
  randomNumber = "Done";

this.getField("Result").value = randomNumber;
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often

20 replies

try67
Community Expert
Community Expert
Nov 13, 2020 10:32 PM UTC

Addendum: How to use the code in a single field with "memory"

Create two text fields, "Roll" and "PastRolls", and a button to update the Roll field with the following script as its Mouse Up action:

 

 

var maxRolls = 20;
var currentRollField = this.getField("Roll");
var pastRollsField = this.getField("PastRolls");
var pastNumbers = pastRollsField.valueAsString == "" ? [] : pastRollsField.valueAsString.split(",");
if (pastNumbers.length==maxRolls) {
	app.alert("No more rolls are possible.");
} else {
	var randomNumber = Math.ceil(Math.random()*maxRolls);
	while (pastNumbers.indexOf(""+randomNumber)!=-1) {
		randomNumber = Math.ceil(Math.random()*maxRolls);
	}
	currentRollField.value = randomNumber;
	pastNumbers.push(randomNumber);
	pastRollsField.value = pastNumbers.join(",");
}

 

 Clicking the button will update the Roll field with a new value, and save it into the PastRolls field, until the latter contains 20 (or whatever is the value of maxRolls) items, and then it will stop updating. You can of course hide the PastRolls field, if you wish. Resetting the form will restart the entire sequence.

 

Edit: fixed a small bug in the code

Translate
try67
Community Expert
Community Expert
Nov 13, 2020 10:37 PM UTC
kiki1986Author
Known Participant
Nov 13, 2020 10:45 PM UTC

That did the trick thank you try67 and thanks everyone else for your help I really appreciate this.

Translate
try67
Community Expert
Community Expert
Nov 13, 2020 4:09 PM UTC

You can do it like this:

 

var pastNumbers = [];
var max = 20;
var randomNumber = Math.ceil(Math.random()*max);
while (pastNumbers.length<max) {
	if (pastNumbers.indexOf(randomNumber)==-1)
		pastNumbers.push(randomNumber);
	randomNumber = Math.ceil(Math.random()*max);
}
console.println(pastNumbers.join(", "));
Translate
kiki1986Author
Known Participant
Nov 13, 2020 4:52 PM UTC

Thank you for that, but where would I put that code?

Translate
ls_rbls
Community Expert
Community Expert
Nov 13, 2020 6:23 PM UTC

Adding to this discussion,

 

See the slide below:

 

Translate
Technical Generalist