Thread overview
why the array bounds array
Dec 08, 2008
Michael P.
Re: why the array bounds error
Dec 08, 2008
Michael P.
Dec 08, 2008
Bill Baxter
Dec 08, 2008
Kagamin
Dec 08, 2008
BCS
Dec 08, 2008
Bill Baxter
Dec 08, 2008
BCS
Dec 08, 2008
Sergey Gromov
Dec 08, 2008
BCS
Dec 09, 2008
Zarathustra
December 08, 2008
Okay, I'm getting an array bounds error, and I have no clue why. Here is the code that affect it:

//Constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int TILE_WIDTH = 20;
const int TILE_HEIGHT = 20; //how big one tile is, in pixels
const int NUMBER_OF_TILES_WIDTH = SCREEN_WIDTH / TILE_WIDTH;
const int NUMBER_OF_TILES_HEIGHT = SCREEN_HEIGHT / TILE_HEIGHT;
const int TYPES_OF_TILES = 4;

//variables
char[ NUMBER_OF_TILES_WIDTH ][ NUMBER_OF_TILES_HEIGHT ] tiles;
//set all tiles to random
for ( int i = 0; i < NUMBER_OF_TILES_WIDTH; i++ )
{
	for ( int j = 0; j < NUMBER_OF_TILES_HEIGHT; j++ )
	{
		tiles[ i ][ j ] = cast( char )( rand() & TYPES_OF_TILES ); //occurs here
	}
}

So, I'm not really sure why it's happening.... Anyone mind shedding some light on why?

-Michael P.
December 08, 2008
Michael P. Wrote:

> Okay, I'm getting an array bounds error, and I have no clue why. Here is the code that affect it:
> 
> //Constants
> const int SCREEN_WIDTH = 640;
> const int SCREEN_HEIGHT = 480;
> const int TILE_WIDTH = 20;
> const int TILE_HEIGHT = 20; //how big one tile is, in pixels
> const int NUMBER_OF_TILES_WIDTH = SCREEN_WIDTH / TILE_WIDTH;
> const int NUMBER_OF_TILES_HEIGHT = SCREEN_HEIGHT / TILE_HEIGHT;
> const int TYPES_OF_TILES = 4;
> 
> //variables
> char[ NUMBER_OF_TILES_WIDTH ][ NUMBER_OF_TILES_HEIGHT ] tiles;
> //set all tiles to random
> for ( int i = 0; i < NUMBER_OF_TILES_WIDTH; i++ )
> {
> 	for ( int j = 0; j < NUMBER_OF_TILES_HEIGHT; j++ )
> 	{
> 		tiles[ i ][ j ] = cast( char )( rand() & TYPES_OF_TILES ); //occurs here
> 	}
> }
> 
> So, I'm not really sure why it's happening.... Anyone mind shedding some light on why?
> 
> -Michael P.

I meant array bounds error, not array bounds array. :P
December 08, 2008
On Mon, Dec 8, 2008 at 11:22 AM, Michael P. <baseball.mjp@gmail.com> wrote:
> Michael P. Wrote:
>
>> Okay, I'm getting an array bounds error, and I have no clue why. Here is the code that affect it:
>>
>> //Constants
>> const int SCREEN_WIDTH = 640;
>> const int SCREEN_HEIGHT = 480;
>> const int TILE_WIDTH = 20;
>> const int TILE_HEIGHT = 20; //how big one tile is, in pixels
>> const int NUMBER_OF_TILES_WIDTH = SCREEN_WIDTH / TILE_WIDTH;
>> const int NUMBER_OF_TILES_HEIGHT = SCREEN_HEIGHT / TILE_HEIGHT;
>> const int TYPES_OF_TILES = 4;
>>
>> //variables
>> char[ NUMBER_OF_TILES_WIDTH ][ NUMBER_OF_TILES_HEIGHT ] tiles;
>> //set all tiles to random
>> for ( int i = 0; i < NUMBER_OF_TILES_WIDTH; i++ )
>> {
>>       for ( int j = 0; j < NUMBER_OF_TILES_HEIGHT; j++ )
>>       {
>>               tiles[ i ][ j ] = cast( char )( rand() & TYPES_OF_TILES ); //occurs here
>>       }
>> }
>>
>> So, I'm not really sure why it's happening.... Anyone mind shedding some light on why?
>>
>> -Michael P.
>
> I meant array bounds error, not array bounds array. :P

Your indices are backwards.

--bb
December 08, 2008
Reply to Michael P.,

rand() & TYPES_OF_TILES

never use rand like that (the low order bit on many rands toggles every single time) 

better (I think):

rand() / (TYPES_OF_TILES / RAND_MAX)


December 08, 2008
On Mon, Dec 8, 2008 at 2:57 PM, BCS <ao@pathlink.com> wrote:
> Reply to Michael P.,
>
> rand() & TYPES_OF_TILES
>
> never use rand like that (the low order bit on many rands toggles every
> single time)
> better (I think):
>
> rand() / (TYPES_OF_TILES / RAND_MAX)

That's a divide by zero, so I don't think that's what you meant.

--bb
December 08, 2008
Reply to Bill,

> On Mon, Dec 8, 2008 at 2:57 PM, BCS <ao@pathlink.com> wrote:
> 
>> Reply to Michael P.,
>> 
>> rand() & TYPES_OF_TILES
>> 
>> never use rand like that (the low order bit on many rands toggles
>> every
>> single time)
>> better (I think):
>> rand() / (TYPES_OF_TILES / RAND_MAX)
>> 
> That's a divide by zero, so I don't think that's what you meant.
> 
> --bb
> 

Oops  x-p

rand() / (RAND_MAX / TYPES_OF_TILES)


December 08, 2008
Mon, 8 Dec 2008 06:59:40 +0000 (UTC), BCS wrote:

> Reply to Bill,
> 
>> On Mon, Dec 8, 2008 at 2:57 PM, BCS <ao@pathlink.com> wrote:
>> 
>>> Reply to Michael P.,
>>> 
>>> rand() & TYPES_OF_TILES
>>> 
>>> never use rand like that (the low order bit on many rands toggles
>>> every
>>> single time)
>>> better (I think):
>>> rand() / (TYPES_OF_TILES / RAND_MAX)
>>> 
>> That's a divide by zero, so I don't think that's what you meant.
>> 
>> --bb
>> 
> 
> Oops  x-p
> 
> rand() / (RAND_MAX / TYPES_OF_TILES)

Don't use rand() like this, ever.  ;)  "RAND_MAX / TYPES_OF_TILES" is an integer expression, it rounds down, therefore your formula gives a slightly greater range of values than you expect.  You'll get all sorts of out of bounds errors.

Your best bet is to use a uniform distribution method of the same random number generator implementation.  There is std.random.uniform() in D2's Phobos which serves this purpose.  If you don't have an equivalent, your next stop is "rand() % TYPES_OF_TILES".  It's not strictly uniform but close if TYPES_OF_TILES is small.
December 08, 2008
Reply to Sergey,

> Mon, 8 Dec 2008 06:59:40 +0000 (UTC), BCS wrote:
>>
>>>> better (I think):
>> rand() / (RAND_MAX / TYPES_OF_TILES)
>> 


> Don't use rand() like this, ever.  ;)  "RAND_MAX / TYPES_OF_TILES" is
> an integer expression, it rounds down, therefore your formula gives a
> slightly greater range of values than you expect.  You'll get all
> sorts of out of bounds errors.
>

thus the (I think). I was just to lazy to figure out the rounding.

the idea still work as long as you round up.

rand() / ( RAND_MAX / TYPES_OF_TILES + !!(RAND_MAX % TYPES_OF_TILES))

OTOH using someone else's code that does the same thing is always better.

> Your best bet is to use a uniform distribution method of the same
> random number generator implementation.  There is std.random.uniform()
> in D2's Phobos which serves this purpose.  If you don't have an
> equivalent, your next stop is "rand() % TYPES_OF_TILES".  It's not
> strictly uniform but close if TYPES_OF_TILES is small.

if TYPES_OF_TILES is to small (like 4) it start s getting highly un random again.
> 


December 08, 2008
Michael P. Wrote:

> I meant array bounds error, not array bounds array. :P

http://www.digitalmars.com/d/2.0/arrays.html
learn prefix and postfix syntax.
December 09, 2008
Michael P. Wrote:

> Okay, I'm getting an array bounds error, and I have no clue why. Here is the code that affect it:
> 
> //Constants
> const int SCREEN_WIDTH = 640;
> const int SCREEN_HEIGHT = 480;
> const int TILE_WIDTH = 20;
> const int TILE_HEIGHT = 20; //how big one tile is, in pixels
> const int NUMBER_OF_TILES_WIDTH = SCREEN_WIDTH / TILE_WIDTH;
> const int NUMBER_OF_TILES_HEIGHT = SCREEN_HEIGHT / TILE_HEIGHT;
> const int TYPES_OF_TILES = 4;
> 
> //variables
> char[ NUMBER_OF_TILES_WIDTH ][ NUMBER_OF_TILES_HEIGHT ] tiles;
> //set all tiles to random
> for ( int i = 0; i < NUMBER_OF_TILES_WIDTH; i++ )
> {
> 	for ( int j = 0; j < NUMBER_OF_TILES_HEIGHT; j++ )
> 	{
> 		tiles[ i ][ j ] = cast( char )( rand() & TYPES_OF_TILES ); //occurs here
> 	}
> }
> 
> So, I'm not really sure why it's happening.... Anyone mind shedding some light on why?
> 
> -Michael P.

for ( int i = 0; i < NUMBER_OF_TILES_WIDTH; i++ )
  for( int j = 0; j < NUMBER_OF_TILES_HEIGHT; j++ )
    // tiles[ i ][ j ] = cast( char )( rand() & TYPES_OF_TILES ); //occurs here
    tiles[ j ][ i ] = cast( char )( rand() & TYPES_OF_TILES ); // swop j and i