This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH][BZ 14561] Separate rand and random seeds.


On 05/20/2013 03:06 PM, OndÅej BÃlka wrote:
> Hi, 
> Functions rand calls random which causes seed to be shared between them.
> 
> This patch causes rand and random use separate seeds.
> 
> Comments?
> 
> Ondra
> 
> 	* stdlib/rand.c: Include stdlib/random.c
> 	* stdlib/random.c: Separate rand and random.

This is user visible change and needs a BZ#.

This also needs a test case similar to what Rich posted
with a comment indicating the rationale from the standards
perspective.

Repost with test, BZ#, and fixes below.

> ---
>  stdlib/rand.c   |   14 ++++++--------
>  stdlib/random.c |   17 +++++++++++++++--
>  2 files changed, 21 insertions(+), 10 deletions(-)
> 
> diff --git a/stdlib/rand.c b/stdlib/rand.c
> index 3e0839f..ec10afd 100644
> --- a/stdlib/rand.c
> +++ b/stdlib/rand.c
> @@ -15,14 +15,12 @@
>     License along with the GNU C Library; if not, see
>     <http://www.gnu.org/licenses/>.  */
>  
> -#include <stdlib.h>
> -
> -#undef	rand
>  
>  
>  /* Return a random integer between 0 and RAND_MAX.  */
> -int
> -rand ()
> -{
> -  return (int) __random ();
> -}
> +
> +#define srandom srand
> +#define __srandom __srand
> +#define USE_AS_RAND
> +
> +#include "stdlib/random.c"

My preference would be to avoid the `#define srandom srand'
shuffle and use an explicit macro for that. It makes it clear
that there are other source files including this source file.

e.g.
RANDOM_RET
RANDOM ()
{
...
}

Then here you would define RANDOM_RET, and RANDOM
to be whatever name you wanted.

> diff --git a/stdlib/random.c b/stdlib/random.c
> index 967dec3..ed16abd 100644
> --- a/stdlib/random.c
> +++ b/stdlib/random.c
> @@ -191,6 +191,8 @@ static struct random_data unsafe_state =
>      .end_ptr = &randtbl[sizeof (randtbl) / sizeof (randtbl[0])]
>  };
>  
> +
> +

Extra whitespace?

>  /* POSIX.1c requires that there is mutual exclusion for the `rand' and
>     `srand' functions to prevent concurrent calls from modifying common
>     data.  */
> @@ -214,7 +216,9 @@ __srandom (x)
>  }
>  
>  weak_alias (__srandom, srandom)
> -weak_alias (__srandom, srand)
> +
> +#ifndef USE_AS_RAND
> +
>  
>  /* Initialize the state information in the given array of N bytes for
>     future random number generation.  Based on the number of bytes we
> @@ -277,6 +281,9 @@ __setstate (arg_state)
>  
>  weak_alias (__setstate, setstate)
>  
> +
> +#endif
> +
>  /* If we are using the trivial TYPE_0 R.N.G., just do the old linear
>     congruential bit.  Otherwise, we do our fancy trinomial stuff, which is the
>     same in all the other cases due to all the global variables that have been
> @@ -288,8 +295,13 @@ weak_alias (__setstate, setstate)
>     rear pointers can't wrap on the same call by not testing the rear
>     pointer if the front one has wrapped.  Returns a 31-bit random number.  */
>  
> +#ifdef USE_AS_RAND
> +int
> +rand ()
> +#else
>  long int
>  __random ()
> +#endif

OK.

>  {
>    int32_t retval;
>  
> @@ -301,5 +313,6 @@ __random ()
>  
>    return retval;
>  }
> -
> +#ifndef USE_AS_RAND
>  weak_alias (__random, random)
> +#endif
> 

OK.

Cheers,
Carlos.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]