ES6(JavaScript), 71,69字节
输入和输出是数组,应该与任何元素类型(字符串,数字等)一起使用,只要它们可以与“ ==”进行比较即可。
打高尔夫球
F=s=>(r=[...s]).sort(_=>Math.random()-.5).some((e,i)=>s[i]==e)?F(s):r
测试
F=s=>(r=[...s]).sort(_=>Math.random()-.5).some((e,i)=>s[i]==e)?F(s):r
F(['A','B','C','D'])
Array [ "D", "C", "A", "B" ]
F(['A','B','C','D'])
Array [ "D", "A", "B", "C" ]
F(['A','B','C','D'])
Array [ "C", "D", "B", "A" ]
F(['A','B','C','D'])
Array [ "D", "C", "B", "A" ]
F(['A','B','C','D'])
Array [ "C", "D", "B", "A" ]
互动片段
F=s=>(r=[...s]).sort(_=>Math.random()-.5).some((e,i)=>s[i]==e)?F(s):r
function G() {
console.log(F(T.value.split``).join``);
}
<input id=T value="ABCDEF"><button id=G onclick="G()">GENERATE</button>