Namespaces
Variants
Actions

How to Generate Random Numbers

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.
136 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved