August 01, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | David Nadlinger:
> I'd argue it does with 1024… ;)
Right, I am sorry :-)
Bye,
bearophile
|
August 01, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On 8/1/11, Adam D. Ruppe <destructionator@gmail.com> wrote:
> I'm barely following this thread, but why not:
>
> ===
> import std.random;
> void main() {
> int[] arr;
> foreach(i; 1 .. 100)
> arr ~= uniform(0, 1024);
> }
> ===
>
> ?
>
Ah but nearly every initialization can be converted to a foreach loop. But I'm trying to avoid that and use a simple expression instead. :)
|
August 01, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On 01.08.2011 23:43, Andrej Mitrovic wrote: > 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*. Dunno maybe it's only me, but some years ago when I used C++98 & STL extensively I would find this line a very simple and clean solution. Now time is changing and so on... A thought - would amap as a shorthand for array(map... float your boat? One pair of parens off. -- Dmitry Olshansky |
August 01, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky | > On 01.08.2011 23:43, Andrej Mitrovic wrote:
> > 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*.
>
> Dunno maybe it's only me, but some years ago when I used C++98 & STL
> extensively I would find this line a very simple and clean solution. Now
> time is changing and so on...
> A thought - would amap as a shorthand for array(map... float your boat?
> One pair of parens off.
Yeah. I confess that I don't see much ugly in that solution. It wouldn't hurt my feelings any to have a cleaner solution, but
array(map!"a % 1024"(take(rndGen(), 1024)));
seems fine to me. But maybe I'm just too used to the STL and std.algorithm. Regardless, it's _way_ cleaner than what you can achieve in C++98 trying to do the same thing.
- Jonathan M Davis
|
August 02, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On Mon, 01 Aug 2011 21:43:13 +0200, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> 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*.
If we ever get UFCS that'd be rndGen().take(1024).map!"a % 1024"().array()
Which is a lot better :--)
Without UFCS, well, how would you want it to look? How does it look in other languages?
|
August 02, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pelle | On 8/2/11, Pelle <pelle.mansson@gmail.com> wrote:
> Without UFCS, well, how would you want it to look?
Maybe..
auto max = 1024;
auto len = 1024;
arr = rndRange(max)[0..len];
IOW, using the slice operator instead of a call to array().
|
August 02, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On 02.08.2011 16:08, Andrej Mitrovic wrote: > On 8/2/11, Pelle<pelle.mansson@gmail.com> wrote: >> Without UFCS, well, how would you want it to look? > Maybe.. > > auto max = 1024; > auto len = 1024; > > arr = rndRange(max)[0..len]; > > IOW, using the slice operator instead of a call to array(). It could be done , but IMO a lot of generic code would totally expect slice to 1. return the same type as the range itself (or 100% compatible) 2. most of the time not to allocate -- Dmitry Olshansky |
August 02, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky | Woops you're right about the range type being returned. I guess this is the next best thing: arr = array(rndRange(max, len)); |
August 02, 2011 Re: Generate array of random values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Andrej Mitrovic: > Maybe.. > > auto max = 1024; > auto len = 1024; > > arr = rndRange(max)[0..len]; In my opinion that's not general enough for Phobos, see the N dimensional table() I have explained here: auto arr = table!q{ uniform(0, 1024) }(1024); http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=28543 Bye, bearophile |
Copyright © 1999-2021 by the D Language Foundation