Range of random numbers

bearophile bearophileHUGS at lycos.com
Mon Apr 23 06:52:32 PDT 2012


Joseph Rushton Wakeling:

> struct UniformRange(T1, T2)
> {
> 	T1 _lower;
> 	T2 _upper;
> 	
> 	@property enum bool empty = false;
> 	
> 	this(T1 a, T2 b)
> 	{
> 		_lower = a;
> 		_upper = b;
> 	}
>
> 	@property auto ref front()
> 	{
> 		assert(!empty);
> 		return uniform(_lower, _upper);
> 	}
>
> 	void popFront()
> 	{
> 	}
> }

What about (untested):

auto uniformRange(T1 lower, T2 upper) {
     return count().map!(_ => uniform(lower, upper))();
}

Where count() is just:
http://d.puremagic.com/issues/show_bug.cgi?id=7839

In the meantime cycle([0]) is acceptable but slower (untested):

return cycle([0]).map!(_ => uniform(lower, upper))();

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list