Highlighted

Script to generate random integers and populate List Box

Avatar

Avatar

Bounteum_Theuam

Avatar

Bounteum_Theuam

Bounteum_Theuam

21-06-2016

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;

     result.push(roundNumber);

     form1.RandomNumber.ListBox1.addItem(result.rawValue)

}

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!

BT

Replies

Highlighted

Avatar

Avatar

Magus069

MVP

Total Posts

1.0K

Likes

132

Correct Answer

150

Avatar

Magus069

MVP

Total Posts

1.0K

Likes

132

Correct Answer

150
Magus069
MVP

21-06-2016

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!

Highlighted

Avatar

Avatar

Bounteum_Theuam

Avatar

Bounteum_Theuam

Bounteum_Theuam

21-06-2016

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

Highlighted

Avatar

Avatar

Magus069

MVP

Total Posts

1.0K

Likes

132

Correct Answer

150

Avatar

Magus069

MVP

Total Posts

1.0K

Likes

132

Correct Answer

150
Magus069
MVP

21-06-2016

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

Highlighted

Avatar

Avatar

Bounteum_Theuam

Avatar

Bounteum_Theuam

Bounteum_Theuam

21-06-2016

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