Thread overview |
---|
August 02, 2015 Randomisation of array order | ||||
---|---|---|---|---|
| ||||
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 |
August 02, 2015 Re: Randomisation of array order | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matt | ...And then I realised that I hadn't looked inside std.random. Question solved, because I am a dumbass. |
August 02, 2015 Re: Randomisation of array order | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matt | 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;
}
|
Copyright © 1999-2021 by the D Language Foundation