Chapter 7. Random Numbers
In Chapter 6, you improved get_prices so you could vary the prompt using a lambda, and you put this more general version in input.cpp. In this chapter, you will write an overload of get_prices using “random” numbers and use it to build a small trading app that allows you to sell fictitious stock. Using randomly generated prices means you don’t need to type in numbers to run your program.
If you generate prices that behave like stock prices, you can even try some trading strategies and see how much money you lose (or make). You tried one such strategy in “Using Lambda Captures for Fun and Profit”, finding the profit on the first uptick. I will show you how to generate prices, based on a simplified version of the Black-Scholes equation. This uses a random variable (also called a stochastic variable), representing uncertainty. Many financial institutions use stochastic models to investigate what might happen under different circumstances, such as an interest rate rise.
Note
Most programming languages have a way to generate numbers that appear to be arbitrary so that you are unlikely to be able to guess what number comes next. These numbers are often called pseudorandom, because they come from a mathematical function. If you know its details, you can work out what comes next. The numbers are therefore not truly random but come close enough for games and the like. If you need true randomness, you need to look beyond software—software and code are deterministic, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access