shuffle a character array

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 20 01:02:07 PDT 2016


On Wednesday, 20 July 2016 at 07:49:38 UTC, celavek wrote:

>
> I thought that I could use a dynamic array as a range ...

You can. However, if you take a look at the documentation for 
std.random.randomShuffle [1], you'll find the following 
constraint:

if (isRandomAccessRange!Range);

You can then go to the documentation for 
std.range.primitives.isRandomAccessRange [2], where you'll find 
the following:

"Although char[] and wchar[] (as well as their qualified versions 
including string and wstring) are arrays, isRandomAccessRange 
yields false for them because they use variable-length encodings 
(UTF-8 and UTF-16 respectively). These types are bidirectional 
ranges only."

If you are absolutely, 100% certain that you are dealing with 
ASCII, you can do this:

```
import std.string : representation;
randomShuffle(charArray.representation);

That will give you a ubyte[] for char[] and a ushort[] for 
wchar[].

[1] https://dlang.org/phobos/std_random.html#.randomShuffle
[2] 
https://dlang.org/phobos/std_range_primitives.html#isRandomAccessRange




More information about the Digitalmars-d-learn mailing list