Shuffle a subset of a list [FINALIZED]
+3
−0
Idea shamelessly stolen from caird and rak1507
Shuffle a subset of a list of unique, positive integers with uniform randomness, given the indices of that subset. For example, given the list
Rules
- Every possible rearrangement of the list should have a non-zero chance of being chosen.
- You may use zero- or one-indexing, but please specify which you use.
- The indices in the input are guaranteed to be unique and valid.
- This is [tag:code-golf], so least number of bytes wins.
Test cases
All of these examples use 0-indexing.
List
Indices
Possible output
[1,2,3,4,5,6,7]
[0,1,2,3,4,5,6]
[2,3,6,1,0,4,5], etc.
[1,2,3,4,5,6,7]
[0,2,4,6]
[3,2,7,4,5,6,1], etc.
[93,6,10,1,200,41,78,31,34,27]
[0,3,4,8,9]
[1,6,10,27,93,41,78,31,200,34], etc.
Questions for Meta:
- How do I enforce randomness?
- Is this a dupe?
- Is this uninteresting?
1 comment thread