genericopenlibs/cstdlib/LSTDLIB/RAND.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // FUNCTION
       
    15 // <<rand>>, <<srand>>---pseudo-random numbers
       
    16 // INDEX
       
    17 // rand
       
    18 // INDEX
       
    19 // srand
       
    20 // INDEX
       
    21 // _rand_r
       
    22 // INDEX
       
    23 // _srand_r
       
    24 // ANSI_SYNOPSIS
       
    25 //
       
    26 
       
    27 // #include <stdlib.h>
       
    28 // int rand(void);
       
    29 // void srand(unsigned int <[seed]>);
       
    30 // int _rand_r(void *<[reent]>);
       
    31 // void _srand_r(void *<[reent]>, unsigned int <[seed]>);
       
    32 // TRAD_SYNOPSIS
       
    33 // #include <stdlib.h>
       
    34 // int rand();
       
    35 // void srand(<[seed]>)
       
    36 // unsigned int <[seed]>;
       
    37 // int _rand_r(<[reent]>);
       
    38 // char *<[reent]>
       
    39 // void _srand_r(<[data]>,<[seed]>)
       
    40 // char *<[reent]>;
       
    41 // unsigned int <[seed]>;
       
    42 // <<rand>> returns a different integer each time it is called; each
       
    43 // integer is chosen by an algorithm designed to be unpredictable, so
       
    44 // that you can use <<rand>> when you require a random number.
       
    45 // The algorithm depends on a static variable called the ``random seed'';
       
    46 // starting with a given value of the random seed always produces the
       
    47 // same sequence of numbers in successive calls to <<rand>>.
       
    48 // You can set the random seed using <<srand>>; it does nothing beyond
       
    49 // storing its argument in the static variable used by <<rand>>.  You can
       
    50 // exploit this to make the pseudo-random sequence less predictable, if
       
    51 // you wish, by using some other unpredictable value (often the least
       
    52 // significant parts of a time-varying value) as the random seed before
       
    53 // beginning a sequence of calls to <<rand>>; or, if you wish to ensure
       
    54 // (for example, while debugging) that successive runs of your program
       
    55 // use the same ``random'' numbers, you can use <<srand>> to set the same
       
    56 // random seed at the outset.
       
    57 // <<_rand_r>> and <<_srand_r>> are reentrant versions of <<rand>> and
       
    58 // <<srand>>.  The extra argument <[reent]> is a pointer to a reentrancy
       
    59 // structure.
       
    60 // RETURNS
       
    61 // <<rand>> returns the next pseudo-random integer in sequence; it is a
       
    62 // number between <<0>> and <<RAND_MAX>> (inclusive).
       
    63 // <<srand>> does not return a result.
       
    64 // PORTABILITY
       
    65 // <<rand>> is required by ANSI, but the algorithm for pseudo-random
       
    66 // number generation is not specified; therefore, even if you use
       
    67 // the same random seed, you cannot expect the same sequence of results
       
    68 // on two different systems.
       
    69 // <<rand>> requires no supporting OS subroutines.
       
    70 //
       
    71 
       
    72 #include <e32math.h>
       
    73 #include <stdlib_r.h>
       
    74 
       
    75 extern "C" {
       
    76 
       
    77 /**
       
    78 Reentrant versions of rand()
       
    79 @param ptr
       
    80 */
       
    81 EXPORT_C int _rand_r(struct _reent *ptr)
       
    82 	{
       
    83 	return Math::Rand(*(TInt64*)ptr->_next);
       
    84 	}
       
    85 
       
    86 /**
       
    87 Reentrant versions of srand()
       
    88 @param ptr
       
    89 @param seed
       
    90 */
       
    91 EXPORT_C void _srand_r (struct _reent *ptr, unsigned int seed)
       
    92 	{
       
    93 	ptr->_next[0] = seed;
       
    94 	ptr->_next[1] = seed;
       
    95 	}
       
    96 
       
    97 /**
       
    98 Returns a different integer each time it is called; each
       
    99 integer is chosen by an algorithm designed to be unpredictable, so
       
   100 that you can use <<rand>> when you require a random number.
       
   101 @return the next pseudo-random integer in sequence; it is a
       
   102 number between 0 and RAND_MAX (inclusive).
       
   103 */
       
   104 EXPORT_C int rand (void)
       
   105 	{
       
   106 	return _rand_r (_REENT);
       
   107 	}
       
   108 
       
   109 /**
       
   110 Sets the random seed.
       
   111 @param seed seed
       
   112 */
       
   113 EXPORT_C void srand (unsigned int seed)
       
   114 	{
       
   115 	_srand_r (_REENT, seed);
       
   116 	}
       
   117 
       
   118 } // extern "C"