954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Really stuck with this any help would be appreciated

I've been working on this code for a simulation, but strange things keep happening. The trader object is (int buy, int inf, int t1). The values of buy and inf should only be 0 or 1, but i keep getting ridiculously high and even negative values and I cant see why. The t1 ends up being around e^-324 sometimes. can anyone tell me why?

Thanks

J

for(int i=0; i<512; i=i+2) //loops through the pairs of traders
{
    double r1 = rand();
    double r2 = rand();
    double max = RAND_MAX;
    double eta1 = r1/max;
    double eta2 = r2/max;
    // Generates random numbers eta1, eta2 between 0 and 1
    double P1 = order0[i].P_buy(t);
    double P2 = order0[i+1].P_buy(t);

    if( order0[i].getbuy()==1 )//if i has bought
    {
        // and if i+1 has bought as well
        if( order0[i+1].getbuy() == 1)
        {
            //then create new level of hierarchy
            order1[i] = trader1(order0[i], order0[i+1]);
        }
        else if( eta2 <= P2 )// if i+1 has not bought, will buy
        {
            order0[i+1].setbuy(1); //trader buys
            //then create new level of hierarchy
            order1[i] = trader1(order0[i], order0[i+1]);
        }
    }
    // at this point we know i has not bought
    else if( order0[i+1].getbuy() == 1)
    {
        if( eta1 <= P1 )// if i buys
        {
            //then create new level of hierarchy
            order1[i] = trader1(order0[i], order0[i+1]);
        }
    }
    // so neither has bought, see if they enter market
    else if ( eta1 <= P1 && eta2 <= P2 )// if both buy
    {
        order0[i].setbuy(1);
        order0[i+1].setbuy(1);
        //create new level of hierarchy
        order1[i] = trader1(order0[i], order0[i+1]);
    }
    else if ( eta1 <= P1 )//only i buys
    {
        order0[i].setbuy(1);
        order0[i+1].setinf(1);// i+1 is influenced
        order0[i+1].sett1(t);
    }
    else if ( eta2 <= P2 )// only i+1 buys
    {
        order0[i+1].setbuy(1);
        order0[i].setinf(1);// i is influenced
        order0[i].sett1(t);
    }
}
jf404
Newbie Poster
3 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

You need to use your compiler's debugger to step through that code and find out what is causing the problem because it isn't apparent from what you posted.

I can tell you that lines 6 and 7 will produce very very small numbers. The purpose of dividing the return value of rand() by RAND_MAX (which is a huge number) isn't apparent.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thanks for the reply. The thing is whenever I try to debug, the debugger quits... I'm also getting an error message that says 'run time error: the stack around the variable order 0 was corrupted, but i only get this every so often. I really don 't understand...

PS the reason for lines 6 and 7 is to return a probability between 0 and 1, hence dividing by rand max

jf404
Newbie Poster
3 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

What compiler are you using ? The program is probably indexing beyond the bounds of arrays, or the index values are crap. Can't really help much without the whole program.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Hi,

There's a really good chance that the problem is not in the function you posted.
Your compiler is right - your stack IS GETTING CORRUPTED and I can tell this from the intermittent values you describe seeing.

Chances are you have an array allocated on the stack (as in a local var) and you've gone past the end. For this to occur, it's probably happening in a function (or method) being called inside the one you posted. You might want to try a tool like valgrind if you're on Linux/x86.

Regards,
Warwick

wazzam
Newbie Poster
4 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You