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
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 )
That's a nice solution :-)
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.