Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Sandbox

Post History

71%
+3 −0
Sandbox Shuffle a subset of a list [FINALIZED]

posted 3y ago by user‭  ·  edited 11mo ago by trichoplax‭

#5: Post edited by user avatar trichoplax‭ · 2023-06-19T10:26:08Z (11 months ago)
Add finalized tag now that the sandbox can be filtered to exclude tags
Shuffle a subset of a list [FINALIZED]
[Posted](https://codegolf.codidact.com/posts/282497)

<sup>Idea shamelessly stolen from [caird](https://chat.stackexchange.com/transcript/message/58238435#58238435) and [rak1507](https://chat.stackexchange.com/transcript/message/58238593#58238593)</sup>

Shuffle a subset of a list of unique, positive integers with uniform randomness, given the indices of that subset. For example, given the list \$[A, B, C, D, E, F, G, H]\$ and the indices \$[0, 3, 4, 5, 7]\$ (0-indexed), you would extract the list \$[A, D, E, F, H]\$, shuffle it, and insert the shuffled elements back according to the list of indices. Some possible results of this are (elements that stayed in place are bolded)
$$[H, \textbf{B}, \textbf{C}, D, A, E, \textbf{G}, F]$$
$$[A, \textbf{B}, \textbf{C}, D, E, F, \textbf{G}, H]$$
$$[F, \textbf{B}, \textbf{C}, E, A, H, \textbf{G}, D]$$.

## 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?
#4: Post edited by user avatar user‭ · 2021-07-01T21:58:31Z (almost 3 years ago)
  • Shuffle a subset of a list
  • Shuffle a subset of a list [FINALIZED]
  • <sup>Idea shamelessly stolen from [caird](https://chat.stackexchange.com/transcript/message/58238435#58238435) and [rak1507](https://chat.stackexchange.com/transcript/message/58238593#58238593)</sup>
  • Shuffle a subset of a list of unique, positive integers with uniform randomness, given the indices of that subset. For example, given the list \$[A, B, C, D, E, F, G, H]\$ and the indices \$[0, 3, 4, 5, 7]\$ (0-indexed), you would extract the list \$[A, D, E, F, H]\$, shuffle it, and insert the shuffled elements back according to the list of indices. Some possible results of this are (elements that stayed in place are bolded)
  • $$[H, \textbf{B}, \textbf{C}, D, A, E, \textbf{G}, F]$$
  • $$[A, \textbf{B}, \textbf{C}, D, E, F, \textbf{G}, H]$$
  • $$[F, \textbf{B}, \textbf{C}, E, A, H, \textbf{G}, D]$$.
  • ## 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?
  • [Posted](https://codegolf.codidact.com/posts/282497)
  • <sup>Idea shamelessly stolen from [caird](https://chat.stackexchange.com/transcript/message/58238435#58238435) and [rak1507](https://chat.stackexchange.com/transcript/message/58238593#58238593)</sup>
  • Shuffle a subset of a list of unique, positive integers with uniform randomness, given the indices of that subset. For example, given the list \$[A, B, C, D, E, F, G, H]\$ and the indices \$[0, 3, 4, 5, 7]\$ (0-indexed), you would extract the list \$[A, D, E, F, H]\$, shuffle it, and insert the shuffled elements back according to the list of indices. Some possible results of this are (elements that stayed in place are bolded)
  • $$[H, \textbf{B}, \textbf{C}, D, A, E, \textbf{G}, F]$$
  • $$[A, \textbf{B}, \textbf{C}, D, E, F, \textbf{G}, H]$$
  • $$[F, \textbf{B}, \textbf{C}, E, A, H, \textbf{G}, D]$$.
  • ## 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?
#3: Post edited by user avatar user‭ · 2021-06-25T20:38:09Z (almost 3 years ago)
  • Shuffle a contiguous subset of a list
  • Shuffle a subset of a list
#2: Post edited by user avatar user‭ · 2021-06-25T20:32:24Z (almost 3 years ago)
  • Note: I had modified this to use a *contiguous* subset, but am now making it any subset, so this draft will be changed later, I'm just too lazy to make the changes right now.
  • <sup>Idea shamelessly stolen from [caird](https://chat.stackexchange.com/transcript/message/58238435#58238435) and [rak1507](https://chat.stackexchange.com/transcript/message/58238593#58238593)</sup>
  • Shuffle a subset of a list of unique, positive integers randomly, given the start and end indices of that subset. For example, given the list \$[A, B, C, D, E, F, G, H]\$ and the indices 0 and 5 (0-indexed, with an exclusive end), you would extract the sublist \$[A, B, C, D]\$, shuffle it, and insert it back. Some possible results are \$[D, C, B, A, E, F, G, H]\$, \$[D, B, A, C, E, F, G, H]\$, and \$[A, C, B, D, E, F, G, H]\$.
  • ## 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.
  • - You may also choose if the end indices are inclusive or exclusive.
  • - If the end index is exclusive, the start index is guaranteed to be less than the end index. Otherwise, the start index is guaranteed to be less than or equal to the end index (an empty subset). Both indices are also guaranteed to be within the bounds of the list.
  • - This is [tag:code-golf], so least number of bytes wins.
  • ## Test cases
  • All of these use zero-indexing, and the end indices are exclusive.
  • ```
  • List
  • Start,end
  • Possible output
  • [1,2,3,4,5]
  • 2,4
  • [1,2,4,3,5]
  • [1,2,3,4,5]
  • [2,2]
  • [1,2,3,4,5] (only possible output)
  • ```
  • ## Questions for Meta:
  • - How do I enforce randomness?
  • - Is this a dupe?
  • - Is this uninteresting?
  • <sup>Idea shamelessly stolen from [caird](https://chat.stackexchange.com/transcript/message/58238435#58238435) and [rak1507](https://chat.stackexchange.com/transcript/message/58238593#58238593)</sup>
  • Shuffle a subset of a list of unique, positive integers with uniform randomness, given the indices of that subset. For example, given the list \$[A, B, C, D, E, F, G, H]\$ and the indices \$[0, 3, 4, 5, 7]\$ (0-indexed), you would extract the list \$[A, D, E, F, H]\$, shuffle it, and insert the shuffled elements back according to the list of indices. Some possible results of this are (elements that stayed in place are bolded)
  • $$[H, \textbf{B}, \textbf{C}, D, A, E, \textbf{G}, F]$$
  • $$[A, \textbf{B}, \textbf{C}, D, E, F, \textbf{G}, H]$$
  • $$[F, \textbf{B}, \textbf{C}, E, A, H, \textbf{G}, D]$$.
  • ## 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: Initial revision by user avatar user‭ · 2021-06-22T23:31:11Z (almost 3 years ago)
Shuffle a contiguous subset of a list
Note: I had modified this to use a *contiguous* subset, but am now making it any subset, so this draft will be changed later, I'm just too lazy to make the changes right now.

<sup>Idea shamelessly stolen from [caird](https://chat.stackexchange.com/transcript/message/58238435#58238435) and [rak1507](https://chat.stackexchange.com/transcript/message/58238593#58238593)</sup>

Shuffle a subset of a list of unique, positive integers randomly, given the start and end indices of that subset. For example, given the list \$[A, B, C, D, E, F, G, H]\$ and the indices 0 and 5 (0-indexed, with an exclusive end), you would extract the sublist \$[A, B, C, D]\$, shuffle it, and insert it back. Some possible results are \$[D, C, B, A, E, F, G, H]\$, \$[D, B, A, C, E, F, G, H]\$, and \$[A, C, B, D, E, F, G, H]\$.

## 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.
- You may also choose if the end indices are inclusive or exclusive.
- If the end index is exclusive, the start index is guaranteed to be less than the end index. Otherwise, the start index is guaranteed to be less than or equal to the end index (an empty subset). Both indices are also guaranteed to be within the bounds of the list.
- This is [tag:code-golf], so least number of bytes wins.

## Test cases

All of these use zero-indexing, and the end indices are exclusive.
```
List
Start,end
Possible output

[1,2,3,4,5]
2,4
[1,2,4,3,5]

[1,2,3,4,5]
[2,2]
[1,2,3,4,5] (only possible output)
```

## Questions for Meta:
- How do I enforce randomness?
- Is this a dupe?
- Is this uninteresting?