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