diff mbox

nandtest: seed random generator properly

Message ID 1322503912-8221-1-git-send-email-computersforpeace@gmail.com
State New, archived
Headers show

Commit Message

Brian Norris Nov. 28, 2011, 6:11 p.m. UTC
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@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@phytec.de>
Signed-off-by: Brian Norris <computersforpeace@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(-)

Comments

Jan Weitzel Nov. 29, 2011, 7:30 a.m. UTC | #1
Brian Norris <computersforpeace@gmail.com> schrieb am 28.11.2011 19:11:52:

> Von: Brian Norris <computersforpeace@gmail.com>
> An: <linux-mtd@lists.infradead.org>
> Kopie: Brian Norris <computersforpeace@gmail.com>, Artem Bityutskiy 
> <dedekind1@gmail.com>, Jan Weitzel <j.weitzel@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@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@phytec.de>
> Signed-off-by: Brian Norris <computersforpeace@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
>
diff mbox

Patch

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);
+
 	fd = open(argv[optind], O_RDWR);
 	if (fd < 0) {
 		perror("open");