Generate a random number within a specific range

Hi all,
Is there a way to generate a random number within a specific range? For example, I want to generate a random number between 60 to 90. Can I able to do it using Appian functions? Using rand() function I can able to generate a random 2, 3, 4 .... digit numbers but not within a specific range.

Discussion posts and replies are publicly visible

• Hi Brinda,
You can use following formula to achieve what you want;
= min number + tointeger(rand() * (max number - min number))
So in your case this will look like
= 60 + tointeger(rand() * (90-60))
Hope this helps!
• Here's another solution:
For your case ri!upper will be 90 and ri!lower will be 60

with(

local!possibleNumbers: difference(
1+enumerate(ri!upper),
1 + enumerate(ri!lower)
),

local!count: length(local!possibleNumbers),

local!randomIndex: tointeger(local!count*(rand())),

if(local!randomIndex =0,
local!possibleNumbers[tointeger(local!count*(rand()))],

local!possibleNumbers[local!randomIndex]
)

)
• Another work around
(
local!min:50,
local!max:100,
local!occurance:10,
local!arr1:Fixed(rand(local!occurance)*local!max,0),

local!placesToChange:where(local!arr1<local!min),
local!valuesToChange:index(local!arr1,local!placesToChange),
local!updatedValues:local!min+local!valuesToChange,

a!sectionLayout(
label: "Lorem Ipsum",
firstColumnContents: {
a!textField(
label: "minimum number",
labelPosition: "ABOVE",
value:local!min,
saveInto: {},
refreshAfter: "UNFOCUS",
validations: {}
),
a!textField(
label: "maximumn number",
labelPosition: "ABOVE",
value:local!max,
saveInto: {},
refreshAfter: "UNFOCUS",
validations: {}
),
a!textField(
label: "Occurance",
labelPosition: "ABOVE",
value: local!occurance,
saveInto: {},
refreshAfter: "UNFOCUS",
validations: {}
),
a!textField(
label: "Original Random numbers",
labelPosition: "ABOVE",
value:local!arr1,
saveInto: {},
refreshAfter: "UNFOCUS",
validations: {}
),
a!textField(
label: "places to change",
labelPosition: "ABOVE",
value:local!placesToChange,
saveInto: {},
refreshAfter: "UNFOCUS",
validations: {}
),
a!textField(
label: "values to change",
labelPosition: "ABOVE",
value:local!valuesToChange,
saveInto: {},
refreshAfter: "UNFOCUS",
validations: {}
),
a!textField(
labelPosition: "ABOVE",
saveInto: {},
refreshAfter: "UNFOCUS",
validations: {}
),
a!textField(
label: "Updated values",
value:local!updatedValues,
labelPosition: "ABOVE",
saveInto: {},
refreshAfter: "UNFOCUS",
validations: {}
),
a!textField(
labelPosition: "ABOVE",
saveInto: {},
refreshAfter: "UNFOCUS",
validations: {}
)
},

validations: {}
)
)

• Thanks Martin! I tried your method. It worked. Thank you Aloks, Chetany and Ashman. I will try your methods too.
• What if the range is decimal values like -0.001 to +0.001, cause your solution only works for integer number, any help would appreciated

• There's only one rand() function and it always returns a decimal between 0 and 1. So, you have to manipulate that number to get what you need. For example, the following would work for the scenario you described:

`(rand()-0.5)/500`

• The below simple logic helps your requirement:

round((rand()*30),0) + 60

Thanks,

Arun