Want to store random numbers in Array

Vishal40
Tera Contributor

Hello Developers,

On button click ( UI action ) I am trying to generate random numbers between 1 to 100.

I want to store all generated random numbers on the form text field with comma-separated values.

Please help me with how I can do the same.

function selectNumber()
{
var finalArr = [];
var answer = confirm("Are you sure to fill the information");
if (answer==true)
{
var n= 100; // Outbound Limit
var x = Math.floor(Math.random() * n); //Generates random number from 0-n
var num = Number(x);
alert(num);
}

}

 

 

1 ACCEPTED SOLUTION

Try this script. This should fix

function selectNumber() {
{
var n = 5; // Outbound Limit
var x = Math.floor(Math.random() * n); //Generates random number from 0-n
var num = Number(x).toString();
var currentVal = g_form.getValue("u_crossed_number");
var currentArr = currentVal.split(",");
// Reset if the count is greater than Outbound limit
if(currentArr.length >= n || currentArr == "")
currentArr = [];

// Repeat the while loop until the script didn't find the existing number
while (currentArr.indexOf(num) >= 0) {
x = Math.floor(Math.random() * n);
num = Number(x).toString();
}
currentArr.push(num);
g_form.setValue("u_crossed_number",currentArr.join(","));

}

Thank you,
Palani

View solution in original post

20 REPLIES 20

Hello Gunjan,

A random number will only generate when I will click on the button.

A single click will generate a single number. If I click on the button 5 times then only 5 random numbers will get generated in the text field.

45,23,16,67,89

 

If I click 2 more times on that button , above text field will populate:-

45,23,16,67,89, 67,45

 

Martin iTSM
Tera Guru

Hey,

not sure what exactly you`re asking about - the number part or putting it into an UI Action?
The number part could look like this i guess:
---
var sRandomNumbers = randomNumberString();

function randomNumberString()

{
    var finalArr = [];
    var n=100; // # of numbers to generate
    var u=100; // Upper Limit
    for (var i=0; i<n;i++) {
        finalArr.push(Math.floor(Math.random() * u + 1).toString());
    }
    gs.log(finalArr.join());
    return finalArr.join();
}
---

Cheers!

Martin

palanikumar
Giga Sage

Hi,

You can create script like below

replace <field_name> with actual field name to be populated

function selectNumber()
{
   var finalArr = [];
   var answer = confirm("Are you sure to fill the information");
   if (answer==true)
   {
      var n= 100; // Outbound Limit
      var x = Math.floor(Math.random() * n); //Generates random number from 0-n
      var num = Number(x);
      var currentVal = g_form.getValue("<field_name>");
      if(currentVal == "")
         g_form.setValue("<field_name>",num);
      else
         g_form.setValue("<field_name>",currentVal + "," + num);
      alert(num);
   }

}
Thank you,
Palani

Thank you. Mission accomplished. 🙂

Hello Sir,

How can I avoid duplicates from the above code?

Random Numbers should not be repeated.