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 re-run simulations based on a seed. Practically, all the modern programming languages include basic random sequences, at least good enough to make good simulations.
Python includes the random
library. In the following code we can see the basic usage of the library:
Importing the
random
library asrnd
: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 of the random
library, follow the link http://docs.python.org/3.2/library/random.html.
In the case of JavaScript, a more basic random
function is included with the Math.random()
function, however...