Re: Generate array of random values

2011-08-02 Thread David Nadlinger
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

Re: Generate array of random values

2011-08-02 Thread bearophile
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/newsgro

Re: Generate array of random values

2011-08-02 Thread Andrej Mitrovic
Woops you're right about the range type being returned. I guess this is the next best thing: arr = array(rndRange(max, len));

Re: Generate array of random values

2011-08-02 Thread Dmitry Olshansky
On 02.08.2011 16:08, Andrej Mitrovic wrote: On 8/2/11, Pelle 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 gener

Re: Generate array of random values

2011-08-02 Thread Andrej Mitrovic
On 8/2/11, Pelle 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().

Re: Generate array of random values

2011-08-02 Thread Pelle
On Mon, 01 Aug 2011 21:43:13 +0200, 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

Re: Generate array of random values

2011-08-01 Thread Jesse Phillips
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

Re: Generate array of random values

2011-08-01 Thread David Nadlinger
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

Re: Generate array of random values

2011-08-01 Thread Jesse Phillips
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

Re: Generate array of random values

2011-08-01 Thread Jonathan M Davis
> 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 ran

Re: Generate array of random values

2011-08-01 Thread 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 param

Re: Generate array of random values

2011-08-01 Thread Andrej Mitrovic
On 8/1/11, Adam D. Ruppe 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. Bu

Re: Generate array of random values

2011-08-01 Thread bearophile
David Nadlinger: > I'd argue it does with 1024… ;) Right, I am sorry :-) Bye, bearophile

Re: Generate array of random values

2011-08-01 Thread Adam D. Ruppe
I'm barely following this thread, but why not: === import std.random; void main() { int[] arr; foreach(i; 1 .. 100) arr ~= uniform(0, 1024); } === ?

Re: Generate array of random values

2011-08-01 Thread Andrej Mitrovic
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 h

Re: Generate array of random values

2011-08-01 Thread David Nadlinger
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

Re: Generate array of random values

2011-08-01 Thread bearophile
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

Re: Generate array of random values

2011-08-01 Thread David Nadlinger
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))).

Re: Generate array of random values

2011-08-01 Thread Dmitry Olshansky
On 01.08.2011 18:05, Steven Schveighoffer wrote: On Sat, 30 Jul 2011 11:10:38 -0400, 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); })(io

Re: Generate array of random values

2011-08-01 Thread Steven Schveighoffer
On Sat, 30 Jul 2011 11:10:38 -0400, 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

Re: Generate array of random values

2011-07-30 Thread Andrej Mitrovic
Yeah I really like Python's list comprehensions. That's something I'll always miss in D.

Re: Generate array of random values

2011-07-30 Thread bearophile
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){ r

Generate array of random values

2011-07-30 Thread Andrej Mitrovic
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?