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
  • Something like

    fixed(rand()*ri!range, 0)

    should do it.
  • Hello again, Stefan!

    I used this advice to create a random alpha-numeric 17 character string [ the first character is always the number 1, followed by 16 random alpha-numeric characters. Ex: 1UPJY54FL8YP3L760 ]:

    a!localVariables(
      local!alpha: joinarray(
        {
          1,
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          },
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          },
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          },
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          },
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          },
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          },
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          },
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          },
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          },
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          },
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          },
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          },
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          },
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          },
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          },
          {
            if(
              fixed(rand()*2, 0, true),
              fixed(rand()*10, 0, true),
              char(65 + tointeger(rand() * (90-65)))
            )
          }
        }
      ),
      {
        local!alpha
      }
    )

    I want to know if this can be refactored & simplified to repeat this chorus n-times?:

    {
      if(
        fixed(rand()*2, 0, true),
        fixed(rand()*10, 0, true),
        char(65 + tointeger(rand() * (90-65)))
      )
    }

    This is how I imagined it would work (but doesn't):

    a!localVariables(
      local!alpha: reduce(
        if(
          fixed(rand()*2, 0, true),
          fixed(rand()*10, 0, true),
          char(65 + tointeger(rand() * (90-65)))
        ),
        {1},
        enum(16)
      ),
      local!alpha
    )

  • Does this help ?

    a!localVariables(
      local!alpha: joinarray(
        {
          1,
          a!forEach(
            items: enumerate(
              16
            ),
            expression: {
              if(
                fixed(
                  rand() * 2,
                  0,
                  true
                ),
                fixed(
                  rand() * 10,
                  0,
                  true
                ),
                char(
                  65 + tointeger(
                    rand() * (
                      90 - 65
                    )
                  )
                )
              )
            }
          )
        }
      ),
      {
        local!alpha
      }
    )

  • This is how you do it. Define your range of available characters first, then select randomly. ceiling() is important to make sure the lowest number is 1.

    a!localVariables(
      local!chars: touniformstring({enumerate(10), char(65 + enumerate(26))}),
      joinarray(
        {
          1,
          index(local!chars, ceiling(rand(16) * 36), "-")
        },
        ""
      )
    )

Reply Children