1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDLIB/RAND.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,118 @@
1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// FUNCTION
1.18 +// <<rand>>, <<srand>>---pseudo-random numbers
1.19 +// INDEX
1.20 +// rand
1.21 +// INDEX
1.22 +// srand
1.23 +// INDEX
1.24 +// _rand_r
1.25 +// INDEX
1.26 +// _srand_r
1.27 +// ANSI_SYNOPSIS
1.28 +//
1.29 +
1.30 +// #include <stdlib.h>
1.31 +// int rand(void);
1.32 +// void srand(unsigned int <[seed]>);
1.33 +// int _rand_r(void *<[reent]>);
1.34 +// void _srand_r(void *<[reent]>, unsigned int <[seed]>);
1.35 +// TRAD_SYNOPSIS
1.36 +// #include <stdlib.h>
1.37 +// int rand();
1.38 +// void srand(<[seed]>)
1.39 +// unsigned int <[seed]>;
1.40 +// int _rand_r(<[reent]>);
1.41 +// char *<[reent]>
1.42 +// void _srand_r(<[data]>,<[seed]>)
1.43 +// char *<[reent]>;
1.44 +// unsigned int <[seed]>;
1.45 +// <<rand>> returns a different integer each time it is called; each
1.46 +// integer is chosen by an algorithm designed to be unpredictable, so
1.47 +// that you can use <<rand>> when you require a random number.
1.48 +// The algorithm depends on a static variable called the ``random seed'';
1.49 +// starting with a given value of the random seed always produces the
1.50 +// same sequence of numbers in successive calls to <<rand>>.
1.51 +// You can set the random seed using <<srand>>; it does nothing beyond
1.52 +// storing its argument in the static variable used by <<rand>>. You can
1.53 +// exploit this to make the pseudo-random sequence less predictable, if
1.54 +// you wish, by using some other unpredictable value (often the least
1.55 +// significant parts of a time-varying value) as the random seed before
1.56 +// beginning a sequence of calls to <<rand>>; or, if you wish to ensure
1.57 +// (for example, while debugging) that successive runs of your program
1.58 +// use the same ``random'' numbers, you can use <<srand>> to set the same
1.59 +// random seed at the outset.
1.60 +// <<_rand_r>> and <<_srand_r>> are reentrant versions of <<rand>> and
1.61 +// <<srand>>. The extra argument <[reent]> is a pointer to a reentrancy
1.62 +// structure.
1.63 +// RETURNS
1.64 +// <<rand>> returns the next pseudo-random integer in sequence; it is a
1.65 +// number between <<0>> and <<RAND_MAX>> (inclusive).
1.66 +// <<srand>> does not return a result.
1.67 +// PORTABILITY
1.68 +// <<rand>> is required by ANSI, but the algorithm for pseudo-random
1.69 +// number generation is not specified; therefore, even if you use
1.70 +// the same random seed, you cannot expect the same sequence of results
1.71 +// on two different systems.
1.72 +// <<rand>> requires no supporting OS subroutines.
1.73 +//
1.74 +
1.75 +#include <e32math.h>
1.76 +#include <stdlib_r.h>
1.77 +
1.78 +extern "C" {
1.79 +
1.80 +/**
1.81 +Reentrant versions of rand()
1.82 +@param ptr
1.83 +*/
1.84 +EXPORT_C int _rand_r(struct _reent *ptr)
1.85 + {
1.86 + return Math::Rand(*(TInt64*)ptr->_next);
1.87 + }
1.88 +
1.89 +/**
1.90 +Reentrant versions of srand()
1.91 +@param ptr
1.92 +@param seed
1.93 +*/
1.94 +EXPORT_C void _srand_r (struct _reent *ptr, unsigned int seed)
1.95 + {
1.96 + ptr->_next[0] = seed;
1.97 + ptr->_next[1] = seed;
1.98 + }
1.99 +
1.100 +/**
1.101 +Returns a different integer each time it is called; each
1.102 +integer is chosen by an algorithm designed to be unpredictable, so
1.103 +that you can use <<rand>> when you require a random number.
1.104 +@return the next pseudo-random integer in sequence; it is a
1.105 +number between 0 and RAND_MAX (inclusive).
1.106 +*/
1.107 +EXPORT_C int rand (void)
1.108 + {
1.109 + return _rand_r (_REENT);
1.110 + }
1.111 +
1.112 +/**
1.113 +Sets the random seed.
1.114 +@param seed seed
1.115 +*/
1.116 +EXPORT_C void srand (unsigned int seed)
1.117 + {
1.118 + _srand_r (_REENT, seed);
1.119 + }
1.120 +
1.121 +} // extern "C"