Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
15 // <<rand>>, <<srand>>---pseudo-random numbers
27 // #include <stdlib.h>
29 // void srand(unsigned int <[seed]>);
30 // int _rand_r(void *<[reent]>);
31 // void _srand_r(void *<[reent]>, unsigned int <[seed]>);
33 // #include <stdlib.h>
35 // void srand(<[seed]>)
36 // unsigned int <[seed]>;
37 // int _rand_r(<[reent]>);
39 // void _srand_r(<[data]>,<[seed]>)
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
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.
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.
78 Reentrant versions of rand()
81 EXPORT_C int _rand_r(struct _reent *ptr)
83 return Math::Rand(*(TInt64*)ptr->_next);
87 Reentrant versions of srand()
91 EXPORT_C void _srand_r (struct _reent *ptr, unsigned int seed)
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).
104 EXPORT_C int rand (void)
106 return _rand_r (_REENT);
110 Sets the random seed.
113 EXPORT_C void srand (unsigned int seed)
115 _srand_r (_REENT, seed);