You Are Here:

Community: Wiki

This page was last modified on 9 October 2008, at 17:04.

CS000890 - Random value generation in Open C

From Forum Nokia Wiki



ID CS000890 Creation date April 9, 2008
Platform S60 3rd Edition Tested on devices Nokia N93
Category Open C Subcategory Files/Data


Keywords (APIs, classes, methods, functions): srand(), rand()

Overview

Random value generation in Open C applications can be done with functions called srand() and rand(). Function srand() sets its argument seed as the seed for a new sequence of pseudo-random numbers. Sequences are repeatable by calling srand() with the same seed value. Function srand() needs to be called only once in a program and this should be done before the first call to rand().


Note: In order to use this code, you need to install the Open C plug-in.

This snippet can be self-signed.

MMP file

The following libraries are required:

LIBRARY  libc.lib

Source file

#include <stdio.h>   //printf
#include <stdlib.h> //srand, rand
#include <time.h> //time
 
int LOWER_BOUND = 1;
int UPPER_BOUND = 6;
 
int main (void)
{
int index = 0;
int random_number = 0;
char random_letter = ' ';
time_t now;
 
/* get current time from the system clock */
time(&now);
 
/* set seed for a new sequence of pseudo-random numbers */
srand((unsigned int) now);
 
/* loop 10 rounds to generate different numbers/letters */
for(index = 1; index <= 10; index++)
{
printf("round:%d\n", index);
 
/* get random number between 1 and 6 */
random_number = rand() % (UPPER_BOUND - LOWER_BOUND + 1) + LOWER_BOUND;
printf("random number:[%d]\n", random_number);
 
/* get random character (lowercase letters in the ASCII) */
random_letter = 'a' + rand() % 26;
printf("random letter:[%c]\n", random_letter);
 
printf("- - -\n");
}
 
return 0;
}

Postconditions

10 random numbers (1-6) and 10 lowercase ASCII characters are displayed to standard output.

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fFileE3aMicrokernelE5fArchitectureE2eGIFX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ