Simulations

Overview


Javascript provides a function for generating a random number between {% 0 %} and {% 1 %}.


let test = Math.random();
					

Seeding


For many applications this is sufficient. However, for production quality code, this is often insufficient. This because the code is hard to test. That is, the result of running the code is going to be different with each run, meaning that one cannot just build a unit test which compares the results against a known answer and know that the code is functioning correctly.

This is particularly an issue when the codebase has changed, and you want to re-run your unit tests to verify that code that was working before is still functioning correctly.

The answer is be able to seed the random generator. Seeding is a process whereby you pass a number to the generator, and this fixes the future outcomes to a given stream.

This may seem counter intuitive, because the point of the generator is to be random. However, the resulting stream of numbers, while fixed, for all intents and purposes still looks and functions like a random sequence. But because it is fixed, when we run tests against our code, we can more easily identify bugs.

A good example of a random genearator that can be seeded is provided by David Bau and is hosted here.