Skip to main content
Known Participant
November 13, 2020
Answered

Random number

  • November 13, 2020
  • 2 replies
  • 2701 views

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 has been closed for replies.
Correct answer Thom Parker

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;

2 replies

try67
Community Expert
Community Expert
November 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.");
} 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

try67
Community Expert
Community Expert
November 13, 2020
kiki1986Author
Known Participant
November 13, 2020

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

try67
Community Expert
Community Expert
November 13, 2020

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(", "));
kiki1986Author
Known Participant
November 13, 2020

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

ls_rbls
Community Expert
Community Expert
November 13, 2020

Adding to this discussion,

 

See the slide below: