sl@0: // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // FUNCTION sl@0: // <>, <>---pseudo-random numbers sl@0: // INDEX sl@0: // rand sl@0: // INDEX sl@0: // srand sl@0: // INDEX sl@0: // _rand_r sl@0: // INDEX sl@0: // _srand_r sl@0: // ANSI_SYNOPSIS sl@0: // sl@0: sl@0: // #include sl@0: // int rand(void); sl@0: // void srand(unsigned int <[seed]>); sl@0: // int _rand_r(void *<[reent]>); sl@0: // void _srand_r(void *<[reent]>, unsigned int <[seed]>); sl@0: // TRAD_SYNOPSIS sl@0: // #include sl@0: // int rand(); sl@0: // void srand(<[seed]>) sl@0: // unsigned int <[seed]>; sl@0: // int _rand_r(<[reent]>); sl@0: // char *<[reent]> sl@0: // void _srand_r(<[data]>,<[seed]>) sl@0: // char *<[reent]>; sl@0: // unsigned int <[seed]>; sl@0: // <> returns a different integer each time it is called; each sl@0: // integer is chosen by an algorithm designed to be unpredictable, so sl@0: // that you can use <> when you require a random number. sl@0: // The algorithm depends on a static variable called the ``random seed''; sl@0: // starting with a given value of the random seed always produces the sl@0: // same sequence of numbers in successive calls to <>. sl@0: // You can set the random seed using <>; it does nothing beyond sl@0: // storing its argument in the static variable used by <>. You can sl@0: // exploit this to make the pseudo-random sequence less predictable, if sl@0: // you wish, by using some other unpredictable value (often the least sl@0: // significant parts of a time-varying value) as the random seed before sl@0: // beginning a sequence of calls to <>; or, if you wish to ensure sl@0: // (for example, while debugging) that successive runs of your program sl@0: // use the same ``random'' numbers, you can use <> to set the same sl@0: // random seed at the outset. sl@0: // <<_rand_r>> and <<_srand_r>> are reentrant versions of <> and sl@0: // <>. The extra argument <[reent]> is a pointer to a reentrancy sl@0: // structure. sl@0: // RETURNS sl@0: // <> returns the next pseudo-random integer in sequence; it is a sl@0: // number between <<0>> and <> (inclusive). sl@0: // <> does not return a result. sl@0: // PORTABILITY sl@0: // <> is required by ANSI, but the algorithm for pseudo-random sl@0: // number generation is not specified; therefore, even if you use sl@0: // the same random seed, you cannot expect the same sequence of results sl@0: // on two different systems. sl@0: // <> requires no supporting OS subroutines. sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: sl@0: extern "C" { sl@0: sl@0: /** sl@0: Reentrant versions of rand() sl@0: @param ptr sl@0: */ sl@0: EXPORT_C int _rand_r(struct _reent *ptr) sl@0: { sl@0: return Math::Rand(*(TInt64*)ptr->_next); sl@0: } sl@0: sl@0: /** sl@0: Reentrant versions of srand() sl@0: @param ptr sl@0: @param seed sl@0: */ sl@0: EXPORT_C void _srand_r (struct _reent *ptr, unsigned int seed) sl@0: { sl@0: ptr->_next[0] = seed; sl@0: ptr->_next[1] = seed; sl@0: } sl@0: sl@0: /** sl@0: Returns a different integer each time it is called; each sl@0: integer is chosen by an algorithm designed to be unpredictable, so sl@0: that you can use <> when you require a random number. sl@0: @return the next pseudo-random integer in sequence; it is a sl@0: number between 0 and RAND_MAX (inclusive). sl@0: */ sl@0: EXPORT_C int rand (void) sl@0: { sl@0: return _rand_r (_REENT); sl@0: } sl@0: sl@0: /** sl@0: Sets the random seed. sl@0: @param seed seed sl@0: */ sl@0: EXPORT_C void srand (unsigned int seed) sl@0: { sl@0: _srand_r (_REENT, seed); sl@0: } sl@0: sl@0: } // extern "C"