www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - generate an array of 100 uniform distributed numbers

reply "ddos" <oggs gmx.at> writes:
hi guys, firstly this has no direct application, i'm just playing 
around and learning

i want to create 100 uniform distributed numbers and print them
my first attempt, just written by intuition:
[0 .. 100].map!(v => uniform(0.0, 1.0).writeln);

i found out i can't write [0 .. 100] to define a simple number 
range, but is there a function to do so?

second attempt, replacing the range with an simple array
[0,1,2].map!(v => uniform(0.0,1.0).writeln);
this does compile and run, but doesn't print anything, just an 
empty string, why is that?

finally i got it working with this:
auto t = [0,1,2].map!(v => uniform(0.0,1.0));
writeln(t);

seems pretty easy eh? d is bugging me alot like this ^_^ but i 
love it's syntax
Jan 22 2015
next sibling parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Thursday, 22 January 2015 at 19:26:46 UTC, ddos wrote:
 hi guys, firstly this has no direct application, i'm just 
 playing around and learning

 i want to create 100 uniform distributed numbers and print them
 my first attempt, just written by intuition:
 [0 .. 100].map!(v => uniform(0.0, 1.0).writeln);

 i found out i can't write [0 .. 100] to define a simple number 
 range, but is there a function to do so?
Yes! iota(100)
 second attempt, replacing the range with an simple array
 [0,1,2].map!(v => uniform(0.0,1.0).writeln);
 this does compile and run, but doesn't print anything, just an 
 empty string, why is that?
Maybe you meant to put the .writeln outside of the parens? Since map is lazily evaluated, writeln is never called and the entire expression does nothing.
Jan 22 2015
parent reply "ddos" <oggs gmx.at> writes:
thx, alot :) works as intended

iota(0,100).map!(v => uniform(0.0,1.0)).writeln;
Jan 22 2015
parent "bearophile" <bearophileHUGS lycos.com> writes:
ddos:

 iota(0,100).map!(v => uniform(0.0,1.0)).writeln;
You can also write: 100.iota.map!(_ => uniform01).writeln; Bye, bearophile
Jan 22 2015
prev sibling next sibling parent Justin Whear <justin economicmodeling.com> writes:
On Thu, 22 Jan 2015 19:26:44 +0000, ddos wrote:

 hi guys, firstly this has no direct application, i'm just playing around
 and learning
 
 i want to create 100 uniform distributed numbers and print them my first
 attempt, just written by intuition:
 [0 .. 100].map!(v => uniform(0.0, 1.0).writeln);
 
 i found out i can't write [0 .. 100] to define a simple number range,
 but is there a function to do so?
The iota function from std.range: iota(0, 100).map!(...)
 
 second attempt, replacing the range with an simple array [0,1,2].map!(v
 => uniform(0.0,1.0).writeln);
 this does compile and run, but doesn't print anything, just an empty
 string, why is that?
Two issues: 1) The function supplied to map should be a projection function, e.g. it takes a value and returns a value. Your lambda returns void (the result of writeln). 2) map is lazy--it doesn't do any work until something consumes it. This is awesome for many reasons (e.g. you can process infinite ranges). Nothing in your code is causing the result of map to be consumed, so it does no work.
 finally i got it working with this:
 auto t = [0,1,2].map!(v => uniform(0.0,1.0));
 writeln(t);
This works because writeln eagerly consumes the result of map, causing the work to actually be done. If you like, you can tack the writeln to the end of the pipeline: auto t = [0,1,2].map!(v => uniform(0.0,1.0)).writeln;
Jan 22 2015
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 01/22/2015 11:26 AM, ddos wrote:

 i want to create 100 uniform distributed numbers and print them
 my first attempt, just written by intuition:
 [0 .. 100].map!(v => uniform(0.0, 1.0).writeln);

 i found out i can't write [0 .. 100] to define a simple number range,
As currently being discussed in another thread, the a..b syntax does not correspond to a first-class D language construct.
 but is there a function to do so?
Yes, std.range.iota: auto numbers = iota(100).map!(_ => uniform(0.0, 1.0)); Note that, 'numbers' is lazy, the definition above does not call uniform() yet. To make an array out of it, call std.array.array at the end: auto numbers = iota(100).map!(_ => uniform(0.0, 1.0)).array;
 second attempt, replacing the range with an simple array
 [0,1,2].map!(v => uniform(0.0,1.0).writeln);
 this does compile and run, but doesn't print anything, just an empty
 string, why is that?
What I said above: it is just a range waiting to be used.
 finally i got it working with this:
 auto t = [0,1,2].map!(v => uniform(0.0,1.0));
 writeln(t);

 seems pretty easy eh?
writeln() consumes any range to print it on the standard output. There is no array to speak of though: writeln() consumes a copy of 't' and your 't' is still a lazy range waiting to be consumed. Ali
Jan 22 2015