Antwort: [PATCH] nandtest: seed random generator properly

Jan Weitzel J.Weitzel at phytec.de
Tue Nov 29 02:30:28 EST 2011


Brian Norris <computersforpeace at gmail.com> schrieb am 28.11.2011 19:11:52:

> Von: Brian Norris <computersforpeace at gmail.com>
> An: <linux-mtd at lists.infradead.org>
> Kopie: Brian Norris <computersforpeace at gmail.com>, Artem Bityutskiy 
> <dedekind1 at gmail.com>, Jan Weitzel <j.weitzel at phytec.de>
> Datum: 28.11.2011 19:13
> Betreff: [PATCH] nandtest: seed random generator properly
> 
> This patch fixes two problems in nandtest:
> 
> (1) if a seed is provided it is actually not used. First call is
>     "seed = rand()" killing the given seed.
>     Credit: Jan Weitzel <j.weitzel at phytec.de>
> 
> (2) if a seed is not provided, we use the default rand() values, which
>     produces the same sequence of values every run. It makes more sense
>     to seed with the time to produce more random sequences.
> 
> Cc: Jan Weitzel <j.weitzel at phytec.de>
> Signed-off-by: Brian Norris <computersforpeace at gmail.com>
> ---
> Jan: This is an amendment to your patch. Feel free to add a
> "Signed-off-by" if this works for you.
> 
>  nandtest.c |    6 +++++-
>  1 files changed, 5 insertions(+), 1 deletions(-)
> 
> diff --git a/nandtest.c b/nandtest.c
> index dc28d09..8cdc816 100644
> --- a/nandtest.c
> +++ b/nandtest.c
> @@ -35,7 +35,7 @@ struct mtd_info_user meminfo;
>  struct mtd_ecc_stats oldstats, newstats;
>  int fd;
>  int markbad=0;
> -int seed;
> +int seed = -1;
> 
>  int erase_and_write(loff_t ofs, unsigned char *data, unsigned char 
*rbuf)
>  {
> @@ -192,6 +192,10 @@ int main(int argc, char **argv)
>     if (argc - optind != 1)
>        usage();
> 
> +   if (seed < 0)
> +      seed = time(NULL);
> +   srand(seed);
> +

So you loose all negative seeds. What is about

int seed = time(NULL);
...
case 's'
        seed = atol(optarg);
...
}
srand(seed);

Jan



>     fd = open(argv[optind], O_RDWR);
>     if (fd < 0) {
>        perror("open");
> -- 
> 1.7.5.4
> 




More information about the linux-mtd mailing list