Header Sep
Symbian OS Tips, Tricks & Code
My Rating Score
Login to rate page

September 2005
Methods for generating random numbers in UIQ 2.1

[Back]

Random number generation is an important part of many kinds of applications, for example games. This might be as simple as calling a Symbian OS API for doing this, but there are different options for how to do this, and here we show some methods for generating random numbers.

The following example application simulates one or more dice, and generates random integers in the range 1 to 6:

Download application>>

When the application is started, tapping the smartphone screen will generate a set of random numbers. The following screenshot shows the running application:

Truly random numbers are hard to generate, and would require hardware support, using for example radioactive decay to generate the random numbers. However, pseudo-random numbers can be generated only using software.

In Symbian OS, the following method can be used for random number generation:.

TInt Math::Rand(TInt64& aSeed);

The input parameter to the function should refer to the same variable each time the method is called. However, this seed needs to be initialized one time during the lifetime of a process. If a constant is used for the seed initialization, this will result in that the same sequence of random numbers being generated each time the application is run. This might be useful for some kinds of applications, for example scientific simulations, but for other kinds of applications such as games, it is undesirable to always generate the same random number sequence. One solution to this is to use the current time for the seed.

The Rand() method returns uniformly distributed pseudo-random integers ranging from 0 to KMaxTInt. If random numbers should be given in another range, these numbers can be transformed into that domain in some way. For example, if the numbers should be integers ranging from 1 to 6, the possible outcomes of a dice throw, the following method can be used:

  dicenum = randval % 6 + 1;    

This is shown by the following code for generation of pseudo-random integers in the range 1 to 6:

//Initialization
 TTime now;
 now.HomeTime();
   randSeed = now.Int64();
     Math::Rand( randSeed );

 //Generation of a random numbers:
 for(TInt i = 0 ; i < 100 ; i++)
{
  TInt val = Math::Rand( randSeed ) % 6 + 1;
 }

Another method for generating pseudo-random numbers is to use a mixed congruential generator as follows:

x(n+1) = (a*x(n)+b)) (mod m)

Here, a, b and m are constants which should be carefully chosen since only certain values will give good random numbers. From each value x(n), the next value in the sequence, x(n+1) can be computed.

The following table shows some specific congruential generators: 

Name of source A B   m
Lehmer 23 10^8+1
Rotenberg 2^7+1 2^35
GGL 7^5 0     2^31-1
Neave 131 0   2^35

The UIQ 2.1 SDK documentation does not specify how Math::Rand() generates random numbers, but it might actually use one of these methods. If the application needs to know the exact properties of the generated random numbers, these methods can be directly used.

For example, the following code will use the GGL method for pseudo-random number generation:

//Initialization
 TTime now;
 now.HomeTime();
 TInt seed = now.Int64().Low();
 if(seed < 0 )
 {
  seed = -1 * seed;
 }
 
 m_seed = seed % 50000;

 //Generation of a random numbers:
 for(TInt i = 0 ; i < 100 ; i++)
{
  m_seed = (171*m_seed) % 30269;
  TInt val = m_seed %6 +1;
}

For more information, see the SDK documentation at:
UIQ 2.1 SDK / Developer Library / API Reference / C++ API reference / Maths Services / Math

More information for congruential generators can be found in mathematical texts.

 

 


 

 

My Rating Score
Login to rate page