www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.random.uniform failing

reply remontoir <marc.p.michel gmail.com> writes:
Not really sure what is happening here.
This works is "None" is removed, but displays a silly answer or 
fails with "range is smaller than amount of items to pop" 
otherwise.

I guess it is related to enum being also integers ?

```d
import std.stdio : write, writeln;
import std.random;
import std.algorithm.mutation : remove;

enum Axis { None, X, Y, Z }

void main() {
     Axis[] axes = [Axis.X, Axis.Y, Axis.Z];
     writeln("Original array: ", axes);

     auto axis = axes[uniform(0, axes.length)];
     writeln(axis);
     axes = axes.remove(axis);
     writeln("Array after removing ", axis, " : ", axes);
}

```
Sep 03
parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Tuesday, 3 September 2024 at 11:57:42 UTC, remontoir wrote:
 Not really sure what is happening here.
 This works is "None" is removed, but displays a silly answer or 
 fails with "range is smaller than amount of items to pop" 
 otherwise.

 I guess it is related to enum being also integers ?

 ```d
 import std.stdio : write, writeln;
 import std.random;
 import std.algorithm.mutation : remove;

 enum Axis { None, X, Y, Z }

 void main() {
     Axis[] axes = [Axis.X, Axis.Y, Axis.Z];
     writeln("Original array: ", axes);

     auto axis = axes[uniform(0, axes.length)];
     writeln(axis);
     axes = axes.remove(axis);
     writeln("Array after removing ", axis, " : ", axes);
 }

 ```
I think your wrong about what remove does, `axes=axes.remove(axis.length.uniform)`
Sep 03
parent remontoir <marc.p.michel gmail.com> writes:
On Tuesday, 3 September 2024 at 12:09:36 UTC, monkyyy wrote:

 I think your wrong about what remove does, 
 `axes=axes.remove(axis.length.uniform)`
Duh ! You're right, remove does take indices or ranges. Using filter, now ;) `axes.filter!(a => a != axis).array` thanks.
Sep 03