commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Thomas Neidhart (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (MATH-1161) Feature request: shuffle algorithms for generic types
Date Mon, 03 Nov 2014 12:24:33 GMT

    [ https://issues.apache.org/jira/browse/MATH-1161?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14194497#comment-14194497
] 

Thomas Neidhart commented on MATH-1161:
---------------------------------------

You could also do the following:

{code}
Integer[] a = new Integer[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Collections.shuffle(Arrays.asList(a));
{code}

Imho, this functionality would better be suited in commons-lang.

> 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