Knuth shuffle: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) (→{{header|6502 Assembly}}: Overhauled this section with a solution that can work with any range of values) |
ReeceGoding (talk | contribs) m (→{{header|R}}: Syntax highlighting.) |
||
Line 3,894: | Line 3,894: | ||
===Original Fisher-Yates version=== |
===Original Fisher-Yates version=== |
||
<lang |
<lang rsplus>fisheryatesshuffle <- function(n) |
||
{ |
{ |
||
pool <- seq_len(n) |
pool <- seq_len(n) |
||
Line 3,907: | Line 3,907: | ||
}</lang> |
}</lang> |
||
===Knuth variation=== |
===Knuth variation=== |
||
<lang |
<lang rsplus>fisheryatesknuthshuffle <- function(n) |
||
{ |
{ |
||
a <- seq_len(n) |
a <- seq_len(n) |
||
Line 3,930: | Line 3,930: | ||
===Short version=== |
===Short version=== |
||
After accounting for R being 1-indexed rather than 0-indexed, it's not hard to implement the pseudo-code given in the task almost exactly: |
After accounting for R being 1-indexed rather than 0-indexed, it's not hard to implement the pseudo-code given in the task almost exactly: |
||
<lang |
<lang rsplus>knuth<-function(vec) |
||
{ |
{ |
||
last<-length(vec) |
last<-length(vec) |