Skip to main content
Engineering LibreTexts

9.1: Introduction

  • Page ID
    26407
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The generation of random numbers lends itself to a variety of applications. In general, games rely heavily on random numbers. Without randomization, games would be ultimately predictable, and consequently, boring. It is the unexpected variation which keeps the mind engaged and the player interested. Another area of use is circuit simulation. Even that most simple of components, the resistor, has some level of uncertainty associated with it. For example, we might purchase one thousand 220 ohms resistors rated at 10% tolerance. This means that the acceptable range of variation is \(\pm\) 22 ohms. Consequently, any particular resistor may have a value between 198 and 242 ohms. An obvious question is whether or not this variation will adversely affect a particular circuit design. Because every resistor (along with every other component) will exhibit some variance in its value, Ohm’s law tells us that this will create some variance in the associated voltages and currents. Further, it is not true that if each component has a tolerance of, say 10%, that the overall circuit variation can be no greater than 10%. It is quite possible for multiple 10% tolerance components to be off in such a way that their combined effect will be much greater than 10%.

    For large circuits, computing the worst case variations along with the typical variations caused by component tolerances can be a tedious job. Simulation programs such as Multisim have analyses for just such circumstances. One of particular interest here is called Monte Carlo Analysis. A Monte Carlo analysis randomizes components in a simulation based on their tolerance, performs the simulation, re-randomizes the components to perform another simulation, and on and on for perhaps dozens or even hundreds of simulations. Examination of the results will yield a decent idea of what a typical production spread would be. Our goal in this exercise is to write a very small version of this in order to understand how to use random numbers.

    Before we go any further it is important to note that we will be producing what are properly known as pseudo random sequences, not true random numbers. Truly random numbers are in no way related to each other. That is, given a list of truly random numbers, no pattern or function exists that would allow you to predict with any certainty what the next number will be. Fair coin flips are a good example of the process. The flip of a coin has no bearing on any subsequent flip of the coin. Even if we flip ten heads in a row, the likelihood of the next flip being heads is still 50-50. Because computers are inherently deterministic instead of random, it’s very difficult to get truly random sequences out of them. This deterministic quality is normally a good thing. After all, we tend not to like machines and tools that “behave” in an unpredictable manner. You rightly expect that stepping on the brake will stop your car on dry pavement 100% of the time, not that it will occasionally cause the vehicle to accelerate suddenly, turn on the radio, or cause your windows to open.

    Most computer languages, Python included, use mathematical techniques to create sequences of numbers that appear to be uncorrelated. That is, they appear to be random. If you run the sequence long enough, however, you will note that it repeats. In other words, it becomes predictable, and therefore not random. Properly done, the size of these sequences is very, very long and the values can be treated as random for all but the most sensitive and demanding of cases. (A Monte Carlo analysis would be fine with this.)

    Python does not have a built-in random function. Random functions in Python, like many advanced math functions, are found in external modules. You may think of modules as libraries of code written by other people which you can add to your program. In fact, with continued study you will be able to create your own modules eventually. These modules must be imported into your program before you can use them. The import directives usually occur at the very beginning of the program. To import the random module, use the following code:

    import random
    

    There are functions within the random module that you may find useful. The first is random(). This returns a floating point value between 0 and 1. Consider the following code snippet:

    import random
    
    print( random.random() )
    print( random.random() )
    print( random.random() )
    

    This will print out three fractional values. You might get:

    0.01254372
    0.93470061
    0.50003267
    

    You are just as likely to get some other sequence of three values. Moreover, every time you run the code, you’ll get a different sequence. Another useful function is randrange(). This will produce an integer value between your stated extremes. For example, the following will produce an integer from 0 up to (but not including) 10:

    import random
    
    print( random.randrange(10) )
    

    You can also include a start point (and other limits). The following produces an integer from 2 up to (but not including) 10:

    print( random.randrange(2,10) )
    

    This function is particularly useful if you want to randomly choose one item from a list.

    So, what if we want to pick a random float between two extremes? random.randrange() only produces integers so it won’t work. To do this, we can simply scale the basic random.random() function. Remember that this function will produce a value between 0 and 1. To get a different range, all we have to do is scale and offset the result. For example, if we need a value between 0 and 50, we simply multiply the function by 50.0.

    x = 50.0 * random.random()
    

    What if we want a value between 80 and 100? This requires both scaling and offsetting. First, note that the range of values spans 20 (that is, 100 − 80). So, we scale the function by 20 to give us a random number between 0 and 20. Then we add the lower limit of 80 to offset the result to 80 through 100:

    x = 80.0 + 20.0 * random.random()
    

    In other words, 80 is the lower bound while 20 is the difference between the upper and lower bounds. So the result is that x will be a floating point value between 80 and 100.


    This page titled 9.1: Introduction is shared under a not declared license and was authored, remixed, and/or curated by James M. Fiore.

    • Was this article helpful?