Thanks to rak1507 for the suggestion
"Random" in this challenge always refers to "uniformly random" - of all possible choices, each has an equal chance of being chosen. Uniform shuffling means that every possible permutation of the array has an equal chance of being chosen.
Given an array consisting of positive digits (123456789
), select a randomly chosen, non-empty subsequence from the array, shuffle the elements and reinsert them back into the array in the former indices, outputting the result
For example, take L = [5, 1, 2, 7, 4]
. We choose a random non-empty subsequence, e.g. [5, 2, 4]
. These are at indices 1, 3, 5
(1-indexed). Next, we shuffle [5, 2, 4]
to give e.g. [2, 5, 4]
. We now reinsert these into the list, with 2
at index 1
, 5
at index 3
and 4
at index 5
to give [2, 1, 5, 7, 4]
.
You may also take the input as an integer or a string, and output it as such, or you may mix and match types.
This is code-golf, so the shortest code in bytes wins
subsequence
implies contiguous, you might want to specify that it's all subsets \$\endgroup\$[1, 2, 3]
should be \$\frac{2}{3}\$[1, 2, 3]
; \$\frac{2}{21}\$[2, 1, 3]
; \$\frac{2}{21}\$[1, 3, 2]
; \$\frac{2}{21}\$[3, 2, 1]
; \$\frac{1}{42}\$[3, 1, 2]
; \$\frac{1}{42}\$[2, 3, 1]
. If I was calculated correctly... I would suggest answers show destribution of input[1, 2, 3]
and confirm it meats the requirement of distribution. \$\endgroup\$