Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events

Script to generate random integers and populate List Box


Level 2

This is what I would like the coding to do. On a click, I want the script to generate 4 random integers between 0 and the value that the user enter the field called form1.RandomNumber.NumericField2.rawValue.  Once the random integer is generated, I then would like to store them in an array called "result".  I then would like to display the random population within a List Box called form.RandonNumber.ListBox1.  Below is the script that I wrote:

var result = [ ];

for (var i = 0; i != 4; ++i)


     var randomNumber = Math.random();

     var selectNumber = (randomNumber * form1.RandomNumber.NumericField2.rawValue) + 1;




When the above code is executed, I basically just get the word "Empty" listed within my  List Box.

Any help with this would greatly be appreciated!


4 Replies


Level 10

Hi there,

make sure to use the right variables, yet you declare result as an array, so you can't access the rawValue

yet you push roundNumber within result but roundNumber has never been declared

if you want to have good random results I suggest you multiply the random Number generated by 10, then multiply that result by another (Math.random() * 10)... this will more likely give you a random number between 0 and 99,if you use the method Math.ceil, you will end up having a random number between 1 and 100

Hope this will help!


Level 2

Thank you so much.  It work perfectly.  Just wondering, would you happen to know what I could do to eliminate duplicate record.


Level 10

Using an array to keep each value that has been added to the list, then using the method indexOf to verify if the number has already been inserted once. If the number has already been inserted, decrement the iterator 'i' to test for another number


Level 2

I really appreciated all your help with this.  Thank you!