problem when a map type variable uses an integer created with random() to access its elements
From: jprunier (jprunier_at_xtra.co.nz)
Date: 08/31/04
- Next message: kanze_at_gabi-soft.fr: "Re: export"
- Previous message: kanze_at_gabi-soft.fr: "Re: export"
- Next in thread: Karl Heinz Buchegger: "Re: problem when a map type variable uses an integer created with random() to access its elements"
- Reply: Karl Heinz Buchegger: "Re: problem when a map type variable uses an integer created with random() to access its elements"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 31 Aug 2004 22:02:27 +1200
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
- Next message: kanze_at_gabi-soft.fr: "Re: export"
- Previous message: kanze_at_gabi-soft.fr: "Re: export"
- Next in thread: Karl Heinz Buchegger: "Re: problem when a map type variable uses an integer created with random() to access its elements"
- Reply: Karl Heinz Buchegger: "Re: problem when a map type variable uses an integer created with random() to access its elements"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]