std.random.uniform for enums

Adam D. Ruppe destructionator at gmail.com
Wed Feb 12 18:18:32 PST 2014


On Thursday, 13 February 2014 at 02:02:38 UTC, Anton wrote:
> Am I misunderstanding how this function is meant to be used?

Yeah, uniform takes two numerical arguments: a min and a max. It 
returns a value between the two, including the min, but not 
including the max. So

int a = uniform(0, 10); // returns 0,1,2,3,4,5,6,7,8, or 9.

You could do a random animal by doing `cast(Animals) uniform(0, 
3);`, or getting fancier with reflection stuff... that'd take a 
few more lines, use __traits(getMember) and __traits(allMembers) 
to randomize rather than .min and .max because the latter 
wouldn't handle holes in the values.


More information about the Digitalmars-d-learn mailing list