Randomisation of array order

cym13 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 2 02:32:50 PDT 2015


On Sunday, 2 August 2015 at 09:24:12 UTC, Matt wrote:
> I was planning to use a dynamic array of indices to represent a 
> deck of cards, and was wondering if there was any easy way to 
> "shuffle" the arrays contents? I checked the library docs, but 
> came to the conclusion that sorting arrays is a much more 
> common operation :)
>
> If anyone has a suggestion on a good way to implement this, I'd 
> appreciate it. I don't need you to code it for me ( although I 
> wouldn't turn it down if you've already done it), just a 
> suggestion of what to do would be appreciated

Have you checked std.random ?

     import std.random, std.stdio;

     void main() {
         auto arr = [1, 2, 3, 4, 5];
         arr.randomShuffle;
         arr.writeln;
     }


More information about the Digitalmars-d-learn mailing list