Random Test

To see more JOM Software applications, click here.  


Version History

Version 1.00, release date 2008-04-20.

  • Tested with N93 (pyS60 1.4.1) and E61i (pyS60 1.4.2).

Code Samples

  • How to find out installation directory
  • How to use script name as base for debug filenames
  • How to debug using Trace or Profile
  • How to give function and arguments as arguments for function
  • How to write on appuifw.Text
  • How to measure code section running time
  • Seven different ways to get random integer number from 1-4

Random Integer Test

Python is an interpreted language, which automatically means it's not too fast. Code is read one line at a time, interpreted and only then executes. Being very flexible it allows writing most things in several different ways. This is both an advantage and a disadvantage.

Sometimes you need to have as fast code as possible, especially in time critical sections. How do you do that? You could use some pre-compiled language, but using only python you just have to measure different possibilities and choose the best way for the specific situation. Best is not always fastest.

Some games need to have an element of surprise, within limits of game style and rules. You want to give the player a possibility to learn how computer "thinks", so that he can beat it, but at the same time it cannot be made too easy. You need some randomness.

Python offers several ways to get a "random" integer. The more random the value is, the longer it takes to generate. You have to choose method, which is fast enough and random enough for your specific purpose.

If you need lots of random numbers within as short time period as possible, I recommend random.choice([list]). If you need just one random number every now and then, use time.clock().

random.randint(a, b)

"Return a random integer N such that a <= N <= b."

Very easy to use, supports self-documenting coding. Slowest of all possibilities, calls internally randrange(a,b1).

random.randrange([start,] stop[, step])

"Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn't actually build a range object."

Bit more complex that randint(), but that is no excuse. You gain 10% more speed by using this.


"Return a random element from the non-empty sequence seq."

20% faster that randrange(), also has advantage that the sequence can miss some numbers.


"Return the next random floating point number in the range [0.0, 1.0)."

Each earlier mentioned function calls internally this. The problem is that even thought this is fastest as itself, you need to convert floating point number to integer by yourself. Doing that will lose the speed advantage while reducing the randomness of the result.


"On Unix, return the current processor time as a floating pointnumber expressed in seconds. The precision, and in fact the verydefinition of the meaning of ``processor time'', depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms.

On Windows, this function returns wall-clock seconds elapsed sincethe first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution istypically better than one microsecond."

The fastest, but can be used only occasionally and for a single random number. Completely useless random number generator in loops and if script execution always proceeds the same way. Requires some extra outside randomness, maybe provided by user activity.

My Results