View mode: basic / threaded / horizontal-split · Log in · Help
January 25, 2012
Invalid bounding interval [, ]
I want to fill a ubyte array with random data.
The code compiles with no warnings or errors.

Source snippet:

       auto prng = Random(unpredictableSeed);
       ubyte[] chunk;

       chunk.length = 1024;

       fill(chunk, uniform!("[]")('\x00', '\xFF', prng));

Error (at runtime):

object.Exception@c:\dmd2\windows\bin\..\..\src\phobos\std\random.d(971):
std.random.uniform(): invalid bounding interval [ ,  ]
----------------
423C50
423AC7
404EA8
404EEC
404AE3
4A6109
----------------

Also I lost the URL for this forum, all I see is this nasty PHP News Reader
interface.
Thank you.
January 25, 2012
Re: Invalid bounding interval [, ]
C:

> I want to fill a ubyte array with random data.

In D ubytes are not char, they are two different types. So if you want ubytes, then use ubytes:
uniform!("[]")(ubyte.min, ubyte.max)


Regarding your error, a reduced test case:

import std.random: uniform;
void main() {
   uniform!("[]")(char.min, char.max);
   uniform!("(]")(char.min, char.max);
}

Bye,
bearophile
January 25, 2012
Re: Invalid bounding interval [, ]
On 01/25/2012 04:50 AM, bearophile wrote:
> C:
>
>> I want to fill a ubyte array with random data.
>
> In D ubytes are not char, they are two different types. So if you want ubytes, then use ubytes:
> uniform!("[]")(ubyte.min, ubyte.max)
>
>
> Regarding your error, a reduced test case:
>
> import std.random: uniform;
> void main() {
>      uniform!("[]")(char.min, char.max);
>      uniform!("(]")(char.min, char.max);
> }
>
> Bye,
> bearophile

Even more reduced test case and bug report:
http://d.puremagic.com/issues/show_bug.cgi?id=7367
January 25, 2012
Re: Invalid bounding interval [, ]
On 01/25/2012 04:25 AM, C wrote:
> I want to fill a ubyte array with random data.
> The code compiles with no warnings or errors.
>
> Source snippet:
>
>          auto prng = Random(unpredictableSeed);
>          ubyte[] chunk;
>
>          chunk.length = 1024;
>
>          fill(chunk, uniform!("[]")('\x00', '\xFF', prng));
>
> Error (at runtime):
>
> object.Exception@c:\dmd2\windows\bin\..\..\src\phobos\std\random.d(971):
> std.random.uniform(): invalid bounding interval [ , �]
> ----------------
> 423C50
> 423AC7
> 404EA8
> 404EEC
> 404AE3
> 4A6109
> ----------------
>
> Also I lost the URL for this forum, all I see is this nasty PHP News Reader
> interface.
> Thank you.

The code wouldn't do what you intended even if it compiled. Use this:

auto chunk = new ubyte[1024];
foreach(ref x; chunk) x = uniform!"[]"(ubyte.min, ubyte.max);
January 25, 2012
Re: Invalid bounding interval [, ]
> auto chunk = new ubyte[1024];
> foreach(ref x; chunk) x = uniform!"[]"(ubyte.min, ubyte.max);

Thank you all for your replies.

@ Timon, I have two questions:
1) How come you can omit parentheses for uniform's parameter, shouldn't it be
uniform!("[]")(...) ?
2) Does auto chunk = new ubyte[1024]; ALWAYS create a dynamic array with
changeable length?
That is a silly question but the syntax confuses me.
January 25, 2012
Re: Invalid bounding interval [, ]
On 01/25/2012 12:28 PM, C wrote:
>> auto chunk = new ubyte[1024];
>> foreach(ref x; chunk) x = uniform!"[]"(ubyte.min, ubyte.max);
>
> Thank you all for your replies.
>
> @ Timon, I have two questions:
> 1) How come you can omit parentheses for uniform's parameter, shouldn't it be
>   uniform!("[]")(...) ?

If there is only one template argument, parentheses can be omitted.

> 2) Does auto chunk = new ubyte[1024]; ALWAYS create a dynamic array with
> changeable length?
> That is a silly question but the syntax confuses me.

Yes it does. But this works too and is probably more intuitive if you 
are not familiar with other C-derived languages:

auto chunk = new ubyte[](1024);
Top | Discussion index | About this forum | D home