472,096 Members | 1,386 Online

# problem when a map type variable uses an integer created with random() to access its elements

Hi

I isolated this code which gives me odd results and I am not sure to
understand why. Basically I declared "positions" as a map that I am using to
store pairs of integers. The key used for the map is an integer as well.
When I access the values of a pair by using a "normal" integer as an index
to access the elements positions contains, it works fine. If I use an
integer that I created using the random() function, then the first value
which is return for the pair is this index number and the second is 0.

More practically

int a = 1001;
cout << positions[a].first << " " << positions[a].second << endl; //
will return 1 1 which is correct
int r = (int)(( random() / ( float ) RAND_MAX ) * 1000); // let's say r
= 843
cout << positions[r].first << " " << positions[r].second << endl; //
will return 843 0

Any idea why ?

Here is the complete code ...

int width = 1000;
int height = 1000;
map<const int, pair<int, int> > positions;

int r = (int)(( random() / ( float ) RAND_MAX ) * 1000);

for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
positions[ j * width + i ] = pair<int, int>(i, j);
}
}

cout << a << " " << (int)(positions[r].first) << " " << positions[r].second
<< endl; // doesn't work

Thanks a lot -jp

Jul 22 '05 #1
1 1378
jprunier wrote:

Hi

I isolated this code which gives me odd results and I am not sure to
understand why. Basically I declared "positions" as a map that I am using to
store pairs of integers. The key used for the map is an integer as well.
When I access the values of a pair by using a "normal" integer as an index
to access the elements positions contains, it works fine. If I use an
integer that I created using the random() function, then the first value
which is return for the pair is this index number and the second is 0.

More practically

int a = 1001;
cout << positions[a].first << " " << positions[a].second << endl; //
will return 1 1 which is correct
int r = (int)(( random() / ( float ) RAND_MAX ) * 1000); // let's say r
= 843
cout << positions[r].first << " " << positions[r].second << endl; //
will return 843 0
Lets see:
What is stored in the map at the key of 843?

You stored with
positions[ j * width + i ] = pair<int, int>(i, j);

where width is 1000. So what values are there for i and j such that
j * 1000 + i equals 843?
It turns out that j has to be 0 and i has to be 843.
Thus at map key 843 the pair< 843, 0 > is stored. And that is
exactly what you get from the map.

Seems the problem is with your math and not with the map.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 11 posts views Thread by Faheem Mitha | last post: by 20 posts views Thread by titi | last post: by 33 posts views Thread by abs | last post: by 2 posts views Thread by Robin | last post: by 39 posts views Thread by Martin Jørgensen | last post: by 3 posts views Thread by vezquex | last post: by 18 posts views Thread by atv | last post: by 4 posts views Thread by Gilberto | last post: by 43 posts views Thread by John | last post: by reply views Thread by beacampos | last post: by reply views Thread by isladogs | last post: by 1 post views Thread by beacampos | last post: by reply views Thread by BenitoJuares | last post: by reply views Thread by bakertaylor28 | last post: by reply views Thread by leo001 | last post: by reply views Thread by antdb | last post: by 2 posts views Thread by Bright1Light | last post: by 5 posts views Thread by Bright1Light | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.