- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 04:03 AM
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);
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2022 07:36 AM
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(","));
}
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 04:45 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 04:18 AM
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()
Cheers!
Martin

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 05:06 AM
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);
}
}
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 06:09 AM
Thank you. Mission accomplished. 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 11:29 PM
Hello Sir,
How can I avoid duplicates from the above code?
Random Numbers should not be repeated.