Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Programming with random numbers - yet re-using the same values for testing

There are certain programming jobs where you want to simulate / model random occurrences - for example to shuffle a pack of cards, or (a manpower planning example from many years back) work out whether people leave an organisation duirng a year; it's no good working with percentages as you'll end up with athird of a person here and half a person there when you really want to look at what the organisation might look like. And you cetrainly want random numbers if you're going to run a series of simulations to see how consistent your results are ... this is done with (for example) weather forecasting, where random noise is introduced into readings which are likely to be of high granulatity / limited accuracy.

One of the issues you'll have with "random" values is that they're not really random. For all intents and purposes - under most ciscumstances the built in functions / modules within programming language suffice, but if you're concerend, please do read the individual manuals carefully. But the are, typically, pseudo-random returning a number from a sequence that looks pretty random, and staring from a "seed" point which will differ every time you run the program - usually the start point will be based on the system clock.

A second issue you'll find when using random numbers is how to test your program. You'll get one set of results and perhaps spot what you think is a bug, you'll fix it and say "does it work now". And a test will, indeed work correctly. But you have to ask "did I fix the problem, or did the test just happen not to use that logic because of the sequence of random numbers used?". Fortunatley, in most langauges you can set your own seed, so that you can record the sequence start point and replay the same sequence if the need arises.

[here] is an example, written in Python from yesterdays, Python programming course.

  now = int(time())
  
  # Are we using an old seed
  if len(argv) > 1:
    now = int(argv[1])
    print "replay"
  
  # Set the seed and notify the user
  seed(now)
  print now


The seed normally used is from the system clock (an integer so we don't get rounding issues when reseeding) and it's reported back. Normal runs of the program differ second by second; add the old time value (which was reported back) on the end of the Python command if you want to re-run a previous set of data.
(written 2016-06-22, updated 2016-06-23)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y117 - Python - Already written modules
  [4710] Searching a Json or XML structure for a specific key / value pair in Python - (2016-10-30)
  [4708] Scons - a build system in Python - building hello world - (2016-10-29)
  [4697] Month, Day, Year number to day of week and month names in Python - English and Swedish - (2016-06-23)
  [4452] Binary data handling - Python and Perl - (2015-03-09)
  [4441] Reading command line parameters in Python - (2015-02-23)
  [4086] Cacheing class for Python - using a local SQLite database as a key/value store - (2013-05-14)
  [4085] JSON from Python - first principles, easy example - (2013-05-13)
  [3479] Practical Extraction and Reporting - using Python and Extreme Programming - (2011-10-14)
  [3465] How can I do an FTP transfer in Python? - (2011-10-05)
  [3442] A demonstration of how many Python facilities work together - (2011-09-16)
  [2931] Syncronise - software, trains, and buses. Please! - (2010-08-22)
  [2890] Dates and times in Python - (2010-07-27)
  [2506] Good example of recursion in Python - analyse an RSS feed - (2009-11-18)
  [2020] Learning Python - many new example programs - (2009-01-31)


Back to
Melksham Rail Development Group becoming Melksham Rail Users Group
Previous and next
or
Horse's mouth home
Forward to
Month, Day, Year number to day of week and month names in Python - English and Swedish
Some other Articles
Obfurscated code - it might work, but is it maintainable?
Melksham Rail Development Group - a celebration of 20 years
Lisa writes - on the referendum vote today
Programming with random numbers - yet re-using the same values for testing
Melksham Rail Development Group becoming Melksham Rail Users Group
Supported Bus Services in Wiltshire - analysis of written-in comments
Wiltshire supported bus service consultation, early 2016 - interim results published
How are bus support payments currently calculated?
Real life PHP application using our course training MVC example
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4696_Pro ... sting.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat