• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Random number

Community Beginner ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript

Views

1.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Nov 13, 2020 Nov 13, 2020

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.");
} el
...

Votes

Translate

Translate
Community Expert , Nov 14, 2020 Nov 14, 2020

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")
...

Votes

Translate

Translate
Community Expert ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

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(", "));

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

Adding to this discussion,

 

See the slide below:

Cexpoer2.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

Yes I added that but not sure it does anything.

I put it in text field and I use this code in Button

var a = this.getField("Text1");
var x = Math.floor((Math.random() * 20) + 1);
a.value = x;

Not sure how to proceed here.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

Sorry, you were referring to using a buton my slide doesn't illustrate that. You need to add Try67's code as a Mouse-Up JavaScript event action ion the button field, not the textfield..

 

The code that Try67 provided rolls out all 20 number possibilities all at once though.

 

If this is not what you want then your script or his  need to be slightly modified to meet that condition that you're inquiring about.

 

But anyway, if you're going to use his script it should look like this, and you also make thextfield "Result" multi-line to visually display all values correctly in that field.:

 

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);
}

this.getField("Result").value = pastNumbers.join(", ");

 

To place the code in the button field see slide:

 

button-result.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

Yes , I need one number at a time.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

Just use this code on a button script:

 

this.getField("Result").value  = Math.ceil(Math.random()*20);

 

"Result" is the field name. Change it to match whatever field will show the random number. 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

Thank you Thom, as i explained in my post, I want to show random number between 1 and 20 (one number at a time) but once number is rolled it need be excluded from future rolls.

e.g. roll 15 so 15 can't be in roll again, 10 roll, 10 can't be in roll again ...etc until all numbers rolled.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

Ok, Lets modify Try67's script

 

 

 

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

Numbers are still repeating, e.g. if I roll 5, 5 can still roll again.

I don't think button can hold script like this, perhaps a hidden field that would hold numbers rolled and then stop them from rolling again?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

There are two issue,

1) initialization. You really need a separte button for this. The modified script below will only reset whent he document is closed and reopened. 

2) The loop needs to be stopped from going infinate. 

 

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

this.getField("Result").value = randomNumber;

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

It just shows "Done" and when I reopen file and click button it says "undefined"

here is example  file.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

My appologies. I didn't debug it. Here is the debugged version. It is much simpler and direct than using two fields, but doesn't provide persistence. 

 

 

if(!this.pastNumbers)
    this.pastNumbers = [];
else 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 14, 2020 Nov 14, 2020

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 14, 2020 Nov 14, 2020

Copy link to clipboard

Copied

LATEST

Thank you, that one is working perfectly.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

This is really nice!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines