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 »
Challenges

Shuffle a subset of a list

+5
−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 $[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 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.
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

4 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+1
−0

Japt, 17 bytes

ô@VøYÃíUgV öx)c f

Try it

Japt, 17 bytes

ô@VøYÃíUgV öx)c f

Try it

ô@VøYÃíUgV öx)c f     :Implicit input of arrays U=integers & V=indices
ô                     :Split U on elements ...
 @                    :That return true when their indices are passed through the following function as Y
  VøY                 :  Does V contain Y?
     Ã                :End split
      í               :Interleave with
       UgV            :  Elements in U at indices in V
           öx         :  Random permutation
             )        :End interleave
              c       :Flatten
                f     :Filter
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Jelly, 7 bytes

œPżịẊ¥F

Try it online!

Takes the 1-indexed indices on the left and the list on the right

How it works

œPżịẊ¥F - Main link. Takes I on the left and L on the right
œP      - Partition L after the indices in I, removing the borders
     ¥  - Group the previous 2 links as a dyad f(I, L):
   ị    -   Get the elements of L at the indices in I
    Ẋ   -   Shuffle uniformly
  ż     - Zip
      F - Flatten
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

JavaScript (Node.js), 75 68 bytes

-7 bytes thanks to Hakerh400

(a,b)=>b.map(i=>[a[i],a[j]]=[a[j=b[Math.random()*b.length|0]],a[i]])

Try it online!

Basic random swap algorithm. Unfortunately, JavaScript doesn't have a built in shuffle function.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (3 comments)
+1
−0

APL(Dyalog Unicode), 13 bytes SBCS

{⍵[?⍨≢⍵]}@⎕⊢⎕

Try it on APLgolf!

A tradfn submission which takes both lists on STDIN, second input is 1-indexed.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »