I'm making a corporation simulator, and it contains stocks. In the game you can buy and sell stocks, but I can't implement a realistic change in the price for them. How do I make them change up/down in a realistic way?
Rather unspecific to the programming language or the platform mentioned, but I would probably go for a greater change as a primary depiction of important stock change, and then generate a small fast change upon that to depict the hooky part of the stock graphs.
- You start at time T1. You're going towards time T2. Price at time T1 is P1. P2 is end price.
- You generate a random number (probably based on some events, maybe?) for P2. This is your larger number target price.
You iterate towards it with a timestep. If you update a couple of times (denoted as Tt) between those timesteps, you do
(T2 - T1)/Tt = Your time step
To get the price at this time, you do
P1 + (P2 - P1)/Tt * AmountOfTimeStepsPassed
This will get you a pretty straight line, so you could look into something like Bezier or something to make a curve. But to get those hooks, you need to add a very small totally random deviation upon it. This needs to go from a small negative number to a small positive number!
P1 + (P2 - P1)/Tt * AmountsOfTimeStepsPassed + SmallRandomNumber