[PATCH] nandtest: seed random generator properly

Brian Norris computersforpeace at gmail.com
Mon Nov 28 13:11:52 EST 2011


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




More information about the linux-mtd mailing list