commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mark (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (MATH-1161) Feature request: shuffle algorithms for generic types
Date Sun, 02 Nov 2014 21:30:33 GMT

     [ https://issues.apache.org/jira/browse/MATH-1161?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Mark updated MATH-1161:
-----------------------
    Description: 
The current shuffle algorithm works on int[]. I often need something more generic for arbitrary
array types.

Example:
{code}
import java.util.Random;

public class Shuffle<T> {

    private static final Random rnd = new Random();

    // Implementing Fisher–Yates shuffle
    public void shuffle(T[] ar) {
        for (int i = ar.length - 1; i > 0; i--) {
            int index = rnd.nextInt(i + 1);
            T a = ar[index];
            ar[index] = ar[i];
            ar[i] = a;
        }
    }
}
{code}

  was:
The current shuffle algorithm works on int[]. I often need something more generic for arbitrary
array types.

Example:

import java.util.Random;

public class Shuffle<T> {

    private static final Random rnd = new Random();

    // Implementing Fisher–Yates shuffle
    public void shuffle(T[] ar) {
        for (int i = ar.length - 1; i > 0; i--) {
            int index = rnd.nextInt(i + 1);
            T a = ar[index];
            ar[index] = ar[i];
            ar[i] = a;
        }
    }
}



> Feature request: shuffle algorithms for generic types
> -----------------------------------------------------
>
>                 Key: MATH-1161
>                 URL: https://issues.apache.org/jira/browse/MATH-1161
>             Project: Commons Math
>          Issue Type: Wish
>    Affects Versions: 3.3
>            Reporter: Mark
>            Priority: Minor
>
> The current shuffle algorithm works on int[]. I often need something more generic for
arbitrary array types.
> Example:
> {code}
> import java.util.Random;
> public class Shuffle<T> {
>     private static final Random rnd = new Random();
>     // Implementing Fisher–Yates shuffle
>     public void shuffle(T[] ar) {
>         for (int i = ar.length - 1; i > 0; i--) {
>             int index = rnd.nextInt(i + 1);
>             T a = ar[index];
>             ar[index] = ar[i];
>             ar[i] = a;
>         }
>     }
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message