Thread overview | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 30, 2011 Generate array of random values | ||||
---|---|---|---|---|
| ||||
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? |
July 30, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Andrej Mitrovic: > 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? If you want a single expression you are allowed to write a bit shorter code: auto arr2 = array(map!((int){ return uniform(0, 1024); })(iota(1024))); Once we get amap/afilter added to Phobos (http://d.puremagic.com/issues/show_bug.cgi?id=5756 ) the code gets a bit simpler: auto arr2 = amap!((int){ return uniform(0, 1024); })(iota(1024)); amap/afilter are not orthogonal, but this is acceptable for practicality (and done in all the languages I know), because in D arrays are much more commonly useful than lazy ranges. I have also proposed a N dimensional table() function to be added to Phobos. With it the code gets simple (Mathematica has a similar function): auto arr2 = table!q{ uniform(0, 1024) }(1024); In Python3 you use a list (array) comp: lst = [randrange(1024) for _ in range(1024)] Unfortunately often the "simpler way" in D is to use an imperative programming style. Bye, bearophile |
July 30, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | Yeah I really like Python's list comprehensions. That's something I'll always miss in D. |
August 01, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On Sat, 30 Jul 2011 11:10:38 -0400, Andrej Mitrovic <andrej.mitrovich@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.
-Steve
|
August 01, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 01.08.2011 18:05, Steven Schveighoffer wrote: > On Sat, 30 Jul 2011 11:10:38 -0400, Andrej Mitrovic <andrej.mitrovich@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 |
August 01, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On 7/30/11 5:10 PM, Andrej Mitrovic wrote:
> Is there a simpler way to do get an array of random values?
If you don't need the random numbers to be in a certain range, you could use array(take(rndGen(), 1024)) – if you do need a limit, that would be array(map!"a % 1024"(take(rndGen(), 1024))).
David
|
August 01, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | David Nadlinger:
> if you do need a limit, that would be array(map!"a % 1024"(take(rndGen(), 1024))).
% doesn't give an uniform distribution.
Bye,
bearophile
|
August 01, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 8/1/11 8:55 PM, bearophile wrote:
> David Nadlinger:
>> array(map!"a % 1024"(take(rndGen(), 1024))).
>
> % doesn't give an uniform distribution.
I'd argue it does with 1024… ;)
David
|
August 01, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | Actually I don't really need *uniform* distribution, it's just that when porting C code to D I didn't find any obvious random()/rnd() functions, and uniform seemed to be the closest thing without having to mess around with a bunch of randomization parameters which I don't care about. I don't see how we can claim D to be an elegant language with this mess: array(map!"a % 1024"(take(rndGen(), 1024))) That's just damn *horrible*. |
August 01, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | I'm barely following this thread, but why not: === import std.random; void main() { int[] arr; foreach(i; 1 .. 100) arr ~= uniform(0, 1024); } === ? |
Copyright © 1999-2021 by the D Language Foundation