www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Invalid bounding interval [, ]

reply C <b_avic yahoo.com> writes:
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.
Jan 24 2012
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Jan 24 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
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
Jan 24 2012
prev sibling next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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);
Jan 24 2012
parent reply C <b_avic yahoo.com> writes:
 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.
Jan 25 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
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);
Jan 25 2012
prev sibling parent Era Scarecrow <rtcvb32 yahoo.com> writes:
 1) How come you can omit parentheses for uniform's
 parameter, shouldn't it be
  uniform!("[]")(...) ?
I think I can answer this. With ! already being used, it already knows it's a template and is separating your argument. Being as you only have 1 argument, it's allowed. That's why you can see to!string(x) and to!(string)(x). At least that's how I understand it. If you have more than 1 argument for your template say for 'T2 fromTo(T, T2)(T t)', it has to be in parentheses. Not only would something like 'fromTo!int!string(x)' be difficult to read (and ugly, feels like :: for c++), it would probably be a pain on the compiler and syntax checker. So the second one would be 'fromTo!(int, string)(x)'. Etc. A good number of things are syntactical sugar, making lives easier :)
Jan 25 2012