Stock exchange party calculations
I'm writing a program (http://dancingrobots.org/beurs/) for my youth club that calculates prices of drinks by the number of times it has been bought in the last round (quite a fun party concept). Now the way I calculate the prices is like this:
cola = cola * (vercola / pastVerCola);
Where cola is the price vercola is the times it has been bought this round. pastVerCola is the times it has been bought last round.
开发者_运维知识库A drink can't go under 0.5 euro and above 10 euro.
My two problems are:
- The prices fluctuation is too high (It mostly chances from max to min and reversed)
- If a drink is two times 0 bought, it error's (0/0)
For those intressted here is the full code: http://pastebin.com/PsT2v2Tr
If you want to reduce the fluctuation you can use the square root :
cola = cola * Math.sqrt(vercola / pastVerCola);
Use sqrt multiple times to reduce it even more.
Another way (one I have used in the past) is to dampen fluctuations using a fixed load e.g.
cola = cola * (vercola + 50) / (pastVerCola + 50);
This is useful because it also lets you have an automatic increase/decrease in price with each round (by using 40, 50, 60, etc.)
You can combine these two effects to get the fluctuation you require. e.g.
var VER = 40; // Constant
var PASTVER = 60; // Constant
cola = cola * Math.sqrt((vercola + VER) / (pastVerCola + PASTVER));
By the way, I think the error is caused if no-one buys a particular drink in a round. pastVerCola will be set to zero, giving you a divide by zero error. Using the constants avoids this situation completely.
精彩评论