Knuth shuffle: Difference between revisions

Content added Content deleted
(→‎{{header|6502 Assembly}}: Overhauled this section with a solution that can work with any range of values)
m (→‎{{header|R}}: Syntax highlighting.)
Line 3,894: Line 3,894:
===Original Fisher-Yates version===
===Original Fisher-Yates version===
<lang r>fisheryatesshuffle <- function(n)
<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 r>fisheryatesknuthshuffle <- function(n)
<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 r>knuth<-function(vec)
<lang rsplus>knuth<-function(vec)
{
{
last<-length(vec)
last<-length(vec)