This page was last modified 07:00, 9 April 2008.
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 Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem with running SnapMobileSample | quanxpro | SNAP Mobile | 7 | 2008-04-25 18:27 |
| MMAPI Audio | khalandar | Mobile Java Media (Graphics & Sounds) | 4 | 2007-03-01 06:24 |
| LogExample - logwrap.bib and logwrap.inl | lm.thiago | General Symbian C++ | 4 | 2008-05-08 15:35 |
| a newbie seeks help.... | SubFlame | Mobile Java General | 1 | 2003-02-26 03:34 |
| Which loop is efficient and why? | sharma_durg | Mobile Java General | 11 | 2006-10-10 05:32 |

