Generating random numbers
While getting truly random numbers is a difficult task, most of the Monte Carlo methods perform well with pseudo-random numbers, and this makes it easier to rerun simulations based in a seed. Practically, all modern programming languages include basic random sequences, or at least sequences good enough to produce accurate simulations.
Python includes the
random
library. In the following code, we can see the basic usage of this library:
import random as rnd
Getting a random float between 0 and 1:
>>>rnd.random()
0.254587458742659
Getting a random number between
1
and100
:
>>>rnd.randint(1,100)
56
Getting a random float between
10
and100
using a uniform distribution:
>>>rnd.uniform(10,100)
15.2542689537156
Tip
For a detailed list of methods in the random library, go to: http://docs.python.org/3.2/library/random.html
In the case of JavaScript, a more basic random function is included with the function Math.random()
; for the purpose of...