473,394 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1420
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Faheem Mitha | last post by:
Hi, I'm not sure what would be more appropriate, so I'm ccing it to both alt.comp.lang.learn.c-c++ and comp.lang.python, with followup to alt.comp.lang.learn.c-c++. While working with a...
20
by: titi | last post by:
Question The road and traffic authority for a small country currently uses a system to store information about all 'currently' licensed drivers. A licensed driver has the following info stored...
33
by: abs | last post by:
Hi all. My list: <ul> <li id="a" onclick="show(this)">Aaaaaaaa</li> <li id="b" onclick="show(this)">Bbbbbbbb</li> <li id="c" onclick="show(this)">Cccccccc <ul> <li id="d"...
2
by: Robin | last post by:
H I am trying to use the FileOpen method to read a Random Access file. I first created a structure that looked like this (not completed Public Structure ModelInf Implements ICompare Public...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
3
by: vezquex | last post by:
What I want is an element with a shifting background image: <html><head> <script type="text/javascript"> var x = 0 var y = 0 inc = 4 function bgWander(el){ x = x +...
18
by: atv | last post by:
at least to me it is. I can't figure out for the life what it is i'm doing wrong here. i have a function called assign_coordinate. usually, i pass a malloced pointer to it, then individual...
4
by: Gilberto | last post by:
Hello, I have a couple of forms using the code to FIND AS YOU TYPE from Allen Browne (http://allenbrowne.com/AppFindAsUType.html). It worked PERFECTLY until yesterday when i splitted the db into...
43
by: John | last post by:
Hi This .net is driving me crazy!! In VB6 I had a type which contained a couple of multi-dimentional arrays which i used to create and read records: Type AAA : Array1(10,10,2) as Integer
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.