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);
    }
}

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.

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.