digitalmars.D.learn - why the array bounds array
- Michael P. (21/21) Dec 07 2008 Okay, I'm getting an array bounds error, and I have no clue why. Here is...
- Michael P. (2/27) Dec 07 2008 I meant array bounds error, not array bounds array. :P
- Bill Baxter (3/30) Dec 07 2008 Your indices are backwards.
- Kagamin (3/4) Dec 08 2008 http://www.digitalmars.com/d/2.0/arrays.html
- BCS (6/6) Dec 07 2008 Reply to Michael P.,
- Bill Baxter (3/9) Dec 07 2008 That's a divide by zero, so I don't think that's what you meant.
- BCS (3/19) Dec 07 2008 Oops x-p
- Sergey Gromov (10/32) Dec 08 2008 Don't use rand() like this, ever. ;) "RAND_MAX / TYPES_OF_TILES" is an
- BCS (8/23) Dec 08 2008 thus the (I think). I was just to lazy to figure out the rounding.
- Zarathustra (5/30) Dec 08 2008 for ( int i = 0; i < NUMBER_OF_TILES_WIDTH; i++ )
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.
Dec 07 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
Dec 07 2008
On Mon, Dec 8, 2008 at 11:22 AM, Michael P. <baseball.mjp gmail.com> wrote:Michael P. Wrote:Your indices are backwards. --bbOkay, 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
Dec 07 2008
Michael P. Wrote:I meant array bounds error, not array bounds array. :Phttp://www.digitalmars.com/d/2.0/arrays.html learn prefix and postfix syntax.
Dec 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)
Dec 07 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
Dec 07 2008
Reply to Bill,On Mon, Dec 8, 2008 at 2:57 PM, BCS <ao pathlink.com> wrote:Oops x-p rand() / (RAND_MAX / TYPES_OF_TILES)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
Dec 07 2008
Mon, 8 Dec 2008 06:59:40 +0000 (UTC), BCS wrote:Reply to Bill,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.On Mon, Dec 8, 2008 at 2:57 PM, BCS <ao pathlink.com> wrote:Oops x-p rand() / (RAND_MAX / TYPES_OF_TILES)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
Dec 08 2008
Reply to Sergey,Mon, 8 Dec 2008 06:59:40 +0000 (UTC), BCS wrote:rand() / (RAND_MAX / TYPES_OF_TILES)better (I think):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.
Dec 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.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
Dec 08 2008