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

Post History

66%
+2 −0
Challenges Shuffle a subset of a list

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. Unfortuna...

posted 3y ago by Moshi‭  ·  edited 3y ago by Moshi‭

Answer
#3: Post edited by user avatar Moshi‭ · 2021-07-02T05:13:17Z (almost 3 years ago)
-7 bytes
  • # [JavaScript (Node.js)], 75 bytes
  • ```javascript
  • (a,b,x)=>{for(i of b)x=b[Math.random()*b.length|0],[a[i],a[x]]=[a[x],a[i]]}
  • ```
  • [Try it online!][TIO-kqliws20]
  • Basic random swap algorithm. Unfortunately, JavaScript doesn't have a built in shuffle function.
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kqliws20]: https://tio.run/##dVDLboMwELzzFT7kYKoN4tU8FJE/6BdYq2ZNDHHl4gqsCony7dSmUZtL9@L1zM5qZt/ok4a61x9u29mrWppq4QQSxrg6T43tuWa2YTIeKyleyN2Snrqrfefxk0yM6lp3@0oRBAmNQGJErER4IAA4L7XtBsecGtzAKiYi5kuIDHIooIRn2MEegYkUHiBE@H8u99@HiWPhqWyVpymUGewPUGRQlJDfBUF/gCNihKco8okYD3ZCqtVWzKZ1l1GOCQIm0RsNzOkXplfb61Z3ZDxFyWB0rXj8wzfhWvc@hLVGJca2/CI2059uDl42k5yRbc@hoxkvXjUv3w "JavaScript (Node.js) – Try It Online"
  • # [JavaScript (Node.js)], ~~75~~ 68 bytes
  • -7 bytes thanks to Hakerh400
  • ```javascript
  • (a,b)=>b.map(i=>[a[i],a[j]]=[a[j=b[Math.random()*b.length|0]],a[i]])
  • ```
  • [Try it online!][TIO-kqliws20]
  • Basic random swap algorithm. Unfortunately, JavaScript doesn't have a built in shuffle function.
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kqliws20]: https://tio.run/##dZBNboMwEIX3nMKLLKCaIP7aJIrMDXoCa9SMiSFGBEdgdUM5O7WharOpV8/z3jd6dkufNFaDfth9b65qqfkSEsiIlzK@0yPUvBQkNAKJFpE73XIp3sne4oH6q7mH0YuMO9U39vaVoM9pxGipTD9aZtVoR8aZCJg7QqSQQQ4FvMIbHBCYSOBp5Oj/c5m7PiVOubPSFU8SKFI4HCFPIS8g@wE8f4QTYoDnIKjNwEJfh5l6qxWxad3VKcsEAZPoinrn/DumDzPoRvfUOYvisdOVCqPNr9dv2rR/rOlU3JkmvIjd9MfNvstukjOyfekVzXhx1Lx8Aw "JavaScript (Node.js) – Try It Online"
#2: Post edited by user avatar Moshi‭ · 2021-07-01T23:19:42Z (almost 3 years ago)
Fixed my TIO testing code
  • # [JavaScript (Node.js)], 75 bytes
  • ```javascript
  • (a,b,x)=>{for(i of b)x=b[Math.random()*b.length|0],[a[i],a[x]]=[a[x],a[i]]}
  • ```
  • [Try it online!][TIO-kqliws20]
  • Basic random swap algorithm. Unfortunately, JavaScript doesn't have a built in shuffle function.
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kqliws20]: https://tio.run/##dVBLboMwEN1zillkAdUE8Ws@isgNeoLRqBknQKgojsCKkChnpzap2mzqjcfvM3rPH3KX/tzVN7Nu9aWYy3z2BRUOQX4cS935NegSVDDkit7EXMNO2ov@9IMXFTZFW5nrV8RIQjWj0MCck7vQATzNZ932BkzRmx5yIA/sIYoxwRQzfMUNbhmBInyCmPF/XWKfT4p9aql4sUcRZjFud5jGmGaY/Bicf4d7Zo8Pnmcbge/iuFZLrADGZVdTGCBBUGyDOubwC8u77uqqbqWxlDzw0v1S8JhdSd0UYaMr/0Sr8U8/uQyrUU0M66ObZOKTdU3zNw "JavaScript (Node.js) – Try It Online"
  • # [JavaScript (Node.js)], 75 bytes
  • ```javascript
  • (a,b,x)=>{for(i of b)x=b[Math.random()*b.length|0],[a[i],a[x]]=[a[x],a[i]]}
  • ```
  • [Try it online!][TIO-kqliws20]
  • Basic random swap algorithm. Unfortunately, JavaScript doesn't have a built in shuffle function.
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kqliws20]: https://tio.run/##dVDLboMwELzzFT7kYKoN4tU8FJE/6BdYq2ZNDHHl4gqsCony7dSmUZtL9@L1zM5qZt/ok4a61x9u29mrWppq4QQSxrg6T43tuWa2YTIeKyleyN2Snrqrfefxk0yM6lp3@0oRBAmNQGJErER4IAA4L7XtBsecGtzAKiYi5kuIDHIooIRn2MEegYkUHiBE@H8u99@HiWPhqWyVpymUGewPUGRQlJDfBUF/gCNihKco8okYD3ZCqtVWzKZ1l1GOCQIm0RsNzOkXplfb61Z3ZDxFyWB0rXj8wzfhWvc@hLVGJca2/CI2059uDl42k5yRbc@hoxkvXjUv3w "JavaScript (Node.js) – Try It Online"
#1: Initial revision by user avatar Moshi‭ · 2021-07-01T23:16:41Z (almost 3 years ago)
# [JavaScript (Node.js)], 75 bytes

```javascript
(a,b,x)=>{for(i of b)x=b[Math.random()*b.length|0],[a[i],a[x]]=[a[x],a[i]]}
```

[Try it online!][TIO-kqliws20]

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

[JavaScript (Node.js)]: https://nodejs.org
[TIO-kqliws20]: https://tio.run/##dVBLboMwEN1zillkAdUE8Ws@isgNeoLRqBknQKgojsCKkChnpzap2mzqjcfvM3rPH3KX/tzVN7Nu9aWYy3z2BRUOQX4cS935NegSVDDkit7EXMNO2ov@9IMXFTZFW5nrV8RIQjWj0MCck7vQATzNZ932BkzRmx5yIA/sIYoxwRQzfMUNbhmBInyCmPF/XWKfT4p9aql4sUcRZjFud5jGmGaY/Bicf4d7Zo8Pnmcbge/iuFZLrADGZVdTGCBBUGyDOubwC8u77uqqbqWxlDzw0v1S8JhdSd0UYaMr/0Sr8U8/uQyrUU0M66ObZOKTdU3zNw "JavaScript (Node.js) – Try It Online"