Random Value Generation

Certified Lead Developer
Hi,
I would like to know, is there any function available which can generate Integer random value, based on our specified range. As there is an function Available named as rand() which do not have any parameters, and returns the value of Type Number (Decimal). although we can take out the value after . (dot) and can convert it to an Integer, but in this case also we cannot get the value based on our specified range, so we do not have any control over number of digits gets generated Randomly

For Example: rand() => returns =>           0.6031346

Can be formatted as

load(
local!data: split(tostring(rand()), "."),
tointeger(local!data[2])
)

which returns: 6031346

But here user do not have opportunity to get random numbers within its specified range like,

random(100) => 99, 87 etc.....
random(1000) => 999, 124, 846 etc...

Thanks

OriginalPostID-249106

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    Another option is tointeger(rand()*ri!range) .  If you're going to be using rand() to pick a random item from a list, you should use tointeger(rand()*ri!range +1) to ensure that you don't include 0 in the results (because when you round 0.1242287 to 1's place you get 0) and you do include the top value of your range.  If you don't include the +1 at the end you could find yourself running into errors due to Appian arrays starting at 1.

    You may also, depending on the method you use to convert to integer, see Appian rounding up and already including the top number of your range.  In that case, you may want to reduce your range by 1 so you don't get 11 in your results on occasion when you want 1 - 10.  If it's vitally important that all random numbers be equally likely you may want to refine further how you convert response from rand() to an integer.

Reply
  • 0
    Certified Lead Developer

    Another option is tointeger(rand()*ri!range) .  If you're going to be using rand() to pick a random item from a list, you should use tointeger(rand()*ri!range +1) to ensure that you don't include 0 in the results (because when you round 0.1242287 to 1's place you get 0) and you do include the top value of your range.  If you don't include the +1 at the end you could find yourself running into errors due to Appian arrays starting at 1.

    You may also, depending on the method you use to convert to integer, see Appian rounding up and already including the top number of your range.  In that case, you may want to reduce your range by 1 so you don't get 11 in your results on occasion when you want 1 - 10.  If it's vitally important that all random numbers be equally likely you may want to refine further how you convert response from rand() to an integer.

Children
No Data