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
tointeger(rand(30)*100)
Sometimes it gives duplicates, you want duplicates to be removed too??
thanks for your response, i want to duplicate to be removed too.
I think stefan got the answer for your question.
Look into this thread,
PreetikA15
Just replace the enumerate to 30 instead of 15. Below is the same code what Stefan has mentioned.
a!localVariables( local!vals: tointeger(rand(100) * 50), index( union(local!vals, local!vals), enumerate(30) + 1, null ) )
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.
Peter Lewis 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
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)