29 Dec, 2011, arholly wrote in the 1st comment:
Votes: 0
Hello:
We have code for our stock market on our mud, and it is supposed to have the stocks fluctuate but it seems to only send the stocks skyrocketing up to an insane price. Is there a better way to handle this? We are open to suggestions?
void stock_update()
{
STOCKS *stock;
int gain = 0;
int days;
int flux = dice(1,7) - 4;
int stock_flux = dice(1,9);

for(stock = stock_list; stock; stock = stock->next)
{
if(stock->last_change < current_time - 2*24*60*60)
{
stock->last_change = current_time;
if(stock->phase < 2)
stock->phase++;
else if(stock->phase == 2)
if(flux <= 0)
stock->phase–;
else if(flux <=2)
stock->phase = 2;
else
stock->phase = 0;
else
stock->phase = 0;
}

days = (current_time - stock->last_change)/180;

stock->upordown = number_range(-1, 1);

switch(stock->phase)
{
case 0:
sprintf( log_buf, "STOCK: %s's new price is %d. No movement.", stock->name, stock->cost );
log_string( log_buf );
break;

case 1:
gain = (stock->upordown * stock->cost + stock_flux * stock->cost + days) / 1000;
sprintf( log_buf, "STOCK: %s's move is %d.", stock->name, gain );
log_string( log_buf );
stock->cost += gain;
sprintf( log_buf, "STOCK: %s's new price is %d.", stock->name, stock->cost );
log_string( log_buf );
break;

case 2:
/*
This version is what we'd like to use, but it doesn't compile on
portent.
gain = stock->upordown * stock->cost * log(days + 1) / 100;
*/
/* This is the fallback position for when the preferred doesn't compile. */
gain = ((stock->upordown * stock->cost) * (sqrt(days + stock_flux) *(32/100))) / 1000;
sprintf( log_buf, "STOCK: %s's move is %d.", stock->name, gain );
log_string( log_buf );
stock->cost += gain;
sprintf( log_buf, "STOCK: %s's new price is %d.", stock->name, stock->cost );
log_string( log_buf );

break;

default: break;
}

}
save_stocks();
}
29 Dec, 2011, ocson wrote in the 2nd comment:
Votes: 0
The value of gain after this bit of code

gain = (stock->upordown * stock->cost + stock_flux * stock->cost + days) / 1000;


on line 39 will be a positive number even if stock->upordown is -1.

So case 1 always results in an increase.

EDIT: Slight Correction, the value could be 0, but it won't be negative.
29 Dec, 2011, arholly wrote in the 3rd comment:
Votes: 0
Wow…How'd that get missed. Thanks. Adding parenthesis will help adjust that.
0.0/3