Thread overview | ||||||
---|---|---|---|---|---|---|
|
August 02, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
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));
|
August 02, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | 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
|
August 02, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | 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.
|
August 02, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | 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
|
Copyright © 1999-2021 by the D Language Foundation