OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc1/] [newlib/] [libc/] [stdlib/] [rand.c] - Diff between revs 207 and 345

Only display areas with differences | Details | Blame | View Log

Rev 207 Rev 345
/*
/*
FUNCTION
FUNCTION
<<rand>>, <<srand>>---pseudo-random numbers
<<rand>>, <<srand>>---pseudo-random numbers
 
 
INDEX
INDEX
        rand
        rand
INDEX
INDEX
        srand
        srand
INDEX
INDEX
        rand_r
        rand_r
 
 
ANSI_SYNOPSIS
ANSI_SYNOPSIS
        #include <stdlib.h>
        #include <stdlib.h>
        int rand(void);
        int rand(void);
        void srand(unsigned int <[seed]>);
        void srand(unsigned int <[seed]>);
        int rand_r(unsigned int *<[seed]>);
        int rand_r(unsigned int *<[seed]>);
 
 
TRAD_SYNOPSIS
TRAD_SYNOPSIS
        #include <stdlib.h>
        #include <stdlib.h>
        int rand();
        int rand();
 
 
        void srand(<[seed]>)
        void srand(<[seed]>)
        unsigned int <[seed]>;
        unsigned int <[seed]>;
 
 
        void rand_r(<[seed]>)
        void rand_r(<[seed]>)
        unsigned int *<[seed]>;
        unsigned int *<[seed]>;
 
 
 
 
DESCRIPTION
DESCRIPTION
<<rand>> returns a different integer each time it is called; each
<<rand>> returns a different integer each time it is called; each
integer is chosen by an algorithm designed to be unpredictable, so
integer is chosen by an algorithm designed to be unpredictable, so
that you can use <<rand>> when you require a random number.
that you can use <<rand>> when you require a random number.
The algorithm depends on a static variable called the ``random seed'';
The algorithm depends on a static variable called the ``random seed'';
starting with a given value of the random seed always produces the
starting with a given value of the random seed always produces the
same sequence of numbers in successive calls to <<rand>>.
same sequence of numbers in successive calls to <<rand>>.
 
 
You can set the random seed using <<srand>>; it does nothing beyond
You can set the random seed using <<srand>>; it does nothing beyond
storing its argument in the static variable used by <<rand>>.  You can
storing its argument in the static variable used by <<rand>>.  You can
exploit this to make the pseudo-random sequence less predictable, if
exploit this to make the pseudo-random sequence less predictable, if
you wish, by using some other unpredictable value (often the least
you wish, by using some other unpredictable value (often the least
significant parts of a time-varying value) as the random seed before
significant parts of a time-varying value) as the random seed before
beginning a sequence of calls to <<rand>>; or, if you wish to ensure
beginning a sequence of calls to <<rand>>; or, if you wish to ensure
(for example, while debugging) that successive runs of your program
(for example, while debugging) that successive runs of your program
use the same ``random'' numbers, you can use <<srand>> to set the same
use the same ``random'' numbers, you can use <<srand>> to set the same
random seed at the outset.
random seed at the outset.
 
 
RETURNS
RETURNS
<<rand>> returns the next pseudo-random integer in sequence; it is a
<<rand>> returns the next pseudo-random integer in sequence; it is a
number between <<0>> and <<RAND_MAX>> (inclusive).
number between <<0>> and <<RAND_MAX>> (inclusive).
 
 
<<srand>> does not return a result.
<<srand>> does not return a result.
 
 
NOTES
NOTES
<<rand>> and <<srand>> are unsafe for multi-threaded applications.
<<rand>> and <<srand>> are unsafe for multi-threaded applications.
<<rand_r>> is thread-safe and should be used instead.
<<rand_r>> is thread-safe and should be used instead.
 
 
 
 
PORTABILITY
PORTABILITY
<<rand>> is required by ANSI, but the algorithm for pseudo-random
<<rand>> is required by ANSI, but the algorithm for pseudo-random
number generation is not specified; therefore, even if you use
number generation is not specified; therefore, even if you use
the same random seed, you cannot expect the same sequence of results
the same random seed, you cannot expect the same sequence of results
on two different systems.
on two different systems.
 
 
<<rand>> requires no supporting OS subroutines.
<<rand>> requires no supporting OS subroutines.
*/
*/
 
 
#ifndef _REENT_ONLY
#ifndef _REENT_ONLY
 
 
#include <stdlib.h>
#include <stdlib.h>
#include <reent.h>
#include <reent.h>
 
 
void
void
_DEFUN (srand, (seed), unsigned int seed)
_DEFUN (srand, (seed), unsigned int seed)
{
{
  _REENT_CHECK_RAND48(_REENT);
  _REENT_CHECK_RAND48(_REENT);
  _REENT_RAND_NEXT(_REENT) = seed;
  _REENT_RAND_NEXT(_REENT) = seed;
}
}
 
 
int
int
_DEFUN_VOID (rand)
_DEFUN_VOID (rand)
{
{
  /* This multiplier was obtained from Knuth, D.E., "The Art of
  /* This multiplier was obtained from Knuth, D.E., "The Art of
     Computer Programming," Vol 2, Seminumerical Algorithms, Third
     Computer Programming," Vol 2, Seminumerical Algorithms, Third
     Edition, Addison-Wesley, 1998, p. 106 (line 26) & p. 108 */
     Edition, Addison-Wesley, 1998, p. 106 (line 26) & p. 108 */
  _REENT_CHECK_RAND48(_REENT);
  _REENT_CHECK_RAND48(_REENT);
  _REENT_RAND_NEXT(_REENT) =
  _REENT_RAND_NEXT(_REENT) =
     _REENT_RAND_NEXT(_REENT) * __extension__ 6364136223846793005LL + 1;
     _REENT_RAND_NEXT(_REENT) * __extension__ 6364136223846793005LL + 1;
  return (int)((_REENT_RAND_NEXT(_REENT) >> 32) & RAND_MAX);
  return (int)((_REENT_RAND_NEXT(_REENT) >> 32) & RAND_MAX);
}
}
 
 
#endif /* _REENT_ONLY */
#endif /* _REENT_ONLY */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.