Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Script to generate random integers and populate List Box

Avatar

Avatar
Level 1
Bounteum_Theuam
Level 1

Likes

0 likes

Total Posts

7 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
Bounteum_Theuam
Level 1

Likes

0 likes

Total Posts

7 posts

Correct Reply

0 solutions
View profile
Bounteum_Theuam
Level 1

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

Avatar

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,072 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,072 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile
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!

Avatar

Avatar
Level 1
Bounteum_Theuam
Level 1

Likes

0 likes

Total Posts

7 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
Bounteum_Theuam
Level 1

Likes

0 likes

Total Posts

7 posts

Correct Reply

0 solutions
View profile
Bounteum_Theuam
Level 1

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.

Avatar

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,072 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,072 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile
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

Avatar

Avatar
Level 1
Bounteum_Theuam
Level 1

Likes

0 likes

Total Posts

7 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
Bounteum_Theuam
Level 1

Likes

0 likes

Total Posts

7 posts

Correct Reply

0 solutions
View profile
Bounteum_Theuam
Level 1

21-06-2016

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