wanna fetch random number?

Certified Senior Developer

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

  Discussion posts and replies are publicly visible

Top Replies

Parents
  • 0
    Certified Lead Developer

    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
    )

  • 0
    Certified Lead Developer
    in reply to Mike Schmitt

    That's a nice solution :-)

Reply Children