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 and 100:
>>>rnd.randint(1,100)
56
  • Getting a random float between 10 and 100 using a uniform distribution:
>>>rnd.uniform(10,100)
15.2542689537156

Tip

For a detailed ...

Get Practical Data Analysis - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.