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 Reply
  • Yes. You'll need to do this steps:

    1. get a list of all the Users you want to randomly select - let's call this local!users
    2. derive the number of these - you can use fn!count() over the list from step 1
    3. generate a random number using fn!rand()
    4. multiply the count from step 2 by the value in step 3 - if the result is 0 - because rand() can return 0 - then you'll need to try again - let's call this local!randUserIndex - you'll need to coerce the result into an integer - use fn!integer(fn!fixed()) -  this value represents an random index into your list of users
    5. you can then simply return a user: local!users[local!randUserIndex]

    Hope this helps

    S>

Children