×
Namespaces

Variants
Actions

How to Generate Random Numbers

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: kiran10182 (04 May 2007)
Last edited: hamishwillee (26 Jul 2012)
Reviewer Approved    Thumbs up icon sm.jpg

Following Code snippet shows "How to generate random numbers" with Symbian C++ programming.

Header required:

#include <e32math.h>

Source:

const TUint KRandomNumberSize = 6;
 
TBuf<KRandomNumberSize > CYrAppUi::GenerateRandom()
{
TBuf16<KRandomNumberSize> random(_L(""));
//Generate the random number of 6 Digits
TInt i;
for (i=0; i<KRandomNumberSize ; i++)
{
TInt num = Math::Random() % 10;
TBuf<1> nTmp;
nTmp.Num(num);
random.Append(nTmp);
}
return random;
}

Use it as below:

TBuf<KRandomNumberSize> random(GenerateRandom());

Thus random descriptor will have 6 digits random number. User can change the value of KRandomNumberSize according to his/her needs.

This page was last modified on 26 July 2012, at 05:38.
36 page views in the last 30 days.
×