www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Re: Generate array of random values

reply Jesse Phillips <jessekphillips+D gmail.com> writes:
Andrej Mitrovic 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?

Untested: auto arr = new int[1024]; fill(arr, uniform(0, 1024));
Aug 01 2011
parent reply David Nadlinger <see klickverbot.at> writes:
On 8/2/11 3:40 AM, Jesse Phillips wrote:
 Andrej Mitrovic Wrote:
 Is there a simpler way to do get an array of random values?

Untested: auto arr = new int[1024]; fill(arr, uniform(0, 1024));

This does a great job of creating an array containing the same random value 1024 times. ;) David
Aug 01 2011
parent reply Jesse Phillips <jessekphillips+d gmail.com> writes:
On Tue, 02 Aug 2011 03:48:03 +0200, David Nadlinger wrote:

 On 8/2/11 3:40 AM, Jesse Phillips wrote:
 Andrej Mitrovic Wrote:
 Is there a simpler way to do get an array of random values?

Untested: auto arr = new int[1024]; fill(arr, uniform(0, 1024));

This does a great job of creating an array containing the same random value 1024 times. ;) David

Fine, let me provide a tested one: import std.algorithm; import std.array; import std.random; import std.range; void main() { auto arr = new int[1024]; fill(arr, randomCover(iota(0,1024), rndGen)); } Knew I had done something with fill before.
Aug 01 2011
parent David Nadlinger <see klickverbot.at> writes:
On 8/2/11 6:42 AM, Jesse Phillips wrote:
      auto arr = new int[1024];
      fill(arr, randomCover(iota(0,1024), rndGen));

Note that this produces a random permutation of the numbers 0 to 1023, which may or may not be what you want. David
Aug 02 2011