Skip to main content
preetikam0001
May 16, 2024
Question

wanna fetch random number?

  • May 16, 2024
  • 9 replies
  • 0 views

I have a list of integers from 1 to 100 and i want 30 random numbers from that list how can we do this?

    9 replies

    venkatrea696188
    May 16, 2024

    tointeger(rand(30)*100)

    Sometimes it gives duplicates, you want duplicates to be removed too?? 

    preetikam0001
    May 16, 2024

    thanks for your response, i want to duplicate to be removed too.

    venkatrea696188
    May 16, 2024

    I think stefan got the answer for your question. 

    Look into this thread,

    mikes0011
    Brainy
    May 16, 2024

    step 1: generate list of possible returns (this can be an enumerated list or any other list, for the sake of flexibility)

    step 2: create a "wrapper object" list where each list is assigned a random sort value

    step 3: sort by the sort value, essentially randomizing the list

    step 4: grab the first N results from the randomized list and return [note: paging lets us do this more easily]

    a!localVariables(
      local!myValues: enumerate(100)+1,  /* can be any list */
      local!numberToReturn: 30,  /* can be whatever you want */
      
      local!sortableObjectList: a!forEach(
        local!myValues,
        a!map(
          originalIndex: fv!index,
          sortValue: rand()*10,  /* resolves as decimal between 0 and 10, for readability */
          value: fv!item
        )
      ),
      
      local!sortedPage: todatasubset(
        arrayToPage: local!sortableObjectList,
        pagingConfiguration: a!pagingInfo(
          startIndex: 1,
          batchSize: local!numberToReturn,
          sort: a!sortInfo(
            field: "sortValue",
            ascending: true()
          )
        )
      ),
      
      local!sortedPage.data.value
    )

    The Expression Guru
    stefanhelzle0001
    Brainy
    May 16, 2024

    That's a nice solution :-)

    mikes0011
    Brainy
    May 16, 2024

    thanks!  i believe someone else originally gave me the idea for the assignment of a randomized sort index to the original array - maybe you (not sure, sorry).  Credit where credit is due if it were possible, but I just re-typed it in this case from memory.  I think i'll save it as a tool going forward though, it seems potentially useful.

    The Expression Guru
    mathieud0001
    Brainy
    May 17, 2024

     [mention:6b9f80ff18f9420082d13ef2d5121d56:e9ed411860ed4f2ba0265705b8793d05] had also posted a solution for this problem a while ago I believe

    community.appian.com/.../generate-random-and-unique-numbers-for-a-specific-range

    peter.lewis
    Employee
    May 17, 2024

    Yep the todatasubset() approach that Mike posted is the more general approach to sorting, but the rank() approach from my post works in a few scenarios like this where you're sorting by numbers (and is slightly fewer lines of code) [emoticon:c4563cd7d5574777a71c318021cbbcc8]