Highlighted

Random number

Community Beginner ,
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.

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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;
TOPICS
Acrobat SDK and JavaScript

Views

76

Likes

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

Random number

Community Beginner ,
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.

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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;
TOPICS
Acrobat SDK and JavaScript

Views

77

Likes

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
Nov 13, 2020 0
Most Valuable Participant ,
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(", "));

Likes

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
Reply
Loading...
Nov 13, 2020 0
Community Beginner ,
Nov 13, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Nov 13, 2020 0
Adobe Community Professional ,
Nov 13, 2020

Copy link to clipboard

Copied

Adding to this discussion,

 

See the slide below:

Cexpoer2.png

 

Likes

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
Reply
Loading...
Nov 13, 2020 0
Community Beginner ,
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.

Likes

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
Reply
Loading...
Nov 13, 2020 0
Adobe Community Professional ,
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

Likes

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
Reply
Loading...
Nov 13, 2020 0
Community Beginner ,
Nov 13, 2020

Copy link to clipboard

Copied

Yes , I need one number at a time.

Likes

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
Reply
Loading...
Nov 13, 2020 0
Adobe Community Professional ,
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. 

Likes

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
Reply
Loading...
Nov 13, 2020 1
Community Beginner ,
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.

Likes

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
Reply
Loading...
Nov 13, 2020 0
Adobe Community Professional ,
Nov 13, 2020

Copy link to clipboard

Copied

Ok, Lets modify Try67's script

 

 

 

 

Likes

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
Reply
Loading...
Nov 13, 2020 0
Community Beginner ,
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?

Likes

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
Reply
Loading...
Nov 13, 2020 0
Adobe Community Professional ,
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;

 

Likes

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
Reply
Loading...
Nov 13, 2020 0
Community Beginner ,
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.

Likes

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
Reply
Loading...
Nov 13, 2020 0
Adobe Community Professional ,
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;

 

Likes

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
Reply
Loading...
Nov 13, 2020 0
Community Beginner ,
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?

Likes

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
Reply
Loading...
Nov 13, 2020 0
Adobe Community Professional ,
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;

Likes

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
Reply
Loading...
Nov 14, 2020 1
kiki1986 LATEST
Community Beginner ,
Nov 14, 2020

Copy link to clipboard

Copied

Thank you, that one is working perfectly.

Likes

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
Reply
Loading...
Nov 14, 2020 0
Most Valuable Participant ,
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

Likes

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
Reply
Loading...
Nov 13, 2020 1
Most Valuable Participant ,
Nov 13, 2020

Copy link to clipboard

Copied

Likes

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
Reply
Loading...
Nov 13, 2020 0
Community Beginner ,
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.

Likes

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
Reply
Loading...
Nov 13, 2020 0
Adobe Community Professional ,
Nov 13, 2020

Copy link to clipboard

Copied

This is really nice!

Likes

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
Reply
Loading...
Nov 13, 2020 0