Generate array of random values

Dmitry Olshansky dmitry.olsh at gmail.com
Mon Aug 1 11:34:30 PDT 2011


On 01.08.2011 18:05, Steven Schveighoffer wrote:
> On Sat, 30 Jul 2011 11:10:38 -0400, Andrej Mitrovic 
> <andrej.mitrovich at gmail.com> wrote:
>
>> I'm currently using this:
>>
>> import std.algorithm;
>> import std.array;
>> import std.random;
>> import std.range;
>>
>> void main()
>> {
>>     auto arr2 = array(map!( (int){ return uniform(0, 1024); 
>> })(iota(0, 1024)));
>> }
>>
>> Is there a simpler way to do get an array of random values?
>
> I think that an infinite range which returns random numbers should be 
> a phobos-declared type...  If it's not, it should be.
>
It is, the following e.g. prints 10 random integers:

import std.random, std.algorithm, std.stdio, std.range;

void main()
{
     Xorshift rng;
     rng.seed(unpredictableSeed);
     writeln(take(rng, 10));
}

-- 
Dmitry Olshansky



More information about the Digitalmars-d-learn mailing list