13.07.2015 Views

Analysis of Structures - Baustatik-Info-Server

Analysis of Structures - Baustatik-Info-Server

Analysis of Structures - Baustatik-Info-Server

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2.11. RANDOM NUMBERS Page 35>>> myDict = {’first item’:1, ’second item’:2}>>> print myDict[’second item’]2>>>>>> beatles = {}>>> beatles[’drums’] = ’Ringo’>>> beatles[’bass’] = ’Paul’>>> beatles[’vocal’] = ’John, Paul’>>> beatles[’guitar’] = ’George, John’>>> print beatles{’guitar’: ’George, John’, ’vocal’: ’John, Paul’, ’bass’: ’Paul’,’drums’: ’Ringo’}>>>2.11 Random NumbersBecause a computer is system, which works systematically reproducible, a computer will not be able tocreate real random numbers. If we start a program twice with exact the same input values, the result alsowill be the same. But with a little trick, we can produce so called pseudo random numbers. If we usethe time as input for the initialization <strong>of</strong> our random number generator, we will be sure, that it will beextremely improbable, to get twice the same random number set.So every programing language has it’s own random number generator. Python <strong>of</strong>fers us a build-in package,which is called random. Like in C the random number generator is to initialize with a functionseed(). This function uses the computer time information to make varying starting conditions.The following example will discuss two features <strong>of</strong> the Python library.• How can we use the random package to create random numbers or to create a random order <strong>of</strong>lists, i.e. shuffle the list’s data.• How can we create simple 2d diagram plots using the matplotlib package.The example code consists <strong>of</strong> three sections.• At the beginning the lists xPos and yPos are filled with random numbers. This random x and yvalues are interpreted as point coordinates. Two subsequent points are joint by a line.• Within a second step the list’s element are sorted. The result is plotted again. The result is anincreasing function.• Within the third step the sorted lists are shuffled by the random package. The random order <strong>of</strong> therandom numbers is plotted again.Figure 2.6 shows the walk for random points (blue line), for sorted points (red line) and for shuffledpoints (green line). The left figure shows the content <strong>of</strong> the created png file, the right figure shows theplot within the viewers window.Random Walk visualized with two Lists <strong>of</strong> Random Numbers2.12.2011

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!