From patchwork Wed Nov 30 18:24:49 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Brian Norris X-Patchwork-Id: 128551 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:4978:20e::2]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 5746A1007D1 for ; Thu, 1 Dec 2011 05:26:30 +1100 (EST) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1RVopy-0003pa-CY; Wed, 30 Nov 2011 18:24:54 +0000 Received: from mail-ey0-f177.google.com ([209.85.215.177]) by merlin.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1RVopv-0003p6-EJ for linux-mtd@lists.infradead.org; Wed, 30 Nov 2011 18:24:52 +0000 Received: by eaac10 with SMTP id c10so1271014eaa.36 for ; Wed, 30 Nov 2011 10:24:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=i8puN3DGU6xnKCklzL5vhZBNEMSbIlHx+z4KVioPfLk=; b=DlwuZyL0njLCHbPXUs36t18b19Ac+7NklQOL1zukeQLQwktoULjDtdbUUcNGHePCbM orsTFg5dNtoTLDF8ccsMS6etLvqPrvpWWjS6IXH97sBeBZS/nS53vmT5pWr6C4yX+e54 /6RUaoBMVinfAq/pDhUjuPqTZEs1Pt6z8oj80= MIME-Version: 1.0 Received: by 10.227.205.197 with SMTP id fr5mr1416752wbb.3.1322677489154; Wed, 30 Nov 2011 10:24:49 -0800 (PST) Received: by 10.216.180.195 with HTTP; Wed, 30 Nov 2011 10:24:49 -0800 (PST) In-Reply-To: References: <1322503912-8221-1-git-send-email-computersforpeace@gmail.com> Date: Wed, 30 Nov 2011 10:24:49 -0800 Message-ID: Subject: Re: [PATCH] nandtest: seed random generator properly From: Brian Norris To: Jan Weitzel X-Spam-Note: CRM114 invocation failed X-Spam-Score: -2.7 (--) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-2.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [209.85.215.177 listed in list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (computersforpeace[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature Cc: linux-mtd@lists.infradead.org, Artem Bityutskiy X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-mtd-bounces@lists.infradead.org Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org On Mon, Nov 28, 2011 at 11:30 PM, Jan Weitzel wrote: > Brian Norris schrieb am 28.11.2011 19:11:52: >> +   if (seed < 0) >> +      seed = time(NULL); >> +   srand(seed); > > So you loose all negative seeds. Well, srand() technically takes unsigned ints, so all seeds are technically positive. But that just means we're parsing and storing seeds wrong (not a big deal, really). > What is about > > int seed = time(NULL); > ... > case 's' >        seed = atol(optarg); > ... > } > srand(seed); Not sure if I understand the question perfectly (clarify if I'm wrong). My patch was intended to: (1) use the user-supplied seed (--seed=X) if supplied (2) use a random seed (based on time()) if the seed is left uninitialized (i.e., -1) Anyway, I see a problem with my method. How about the following patch, which saves the time first, then overwrites it with the supplied seed if provided. From 4df0643c4819f5de5db54b855406852da0dc6bf1 Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Mon, 28 Nov 2011 10:03:30 -0800 Subject: [PATCH] nandtest: seed random generator properly This patch fixes two problems in nandtest: * if a seed is provided it is actually not used. First call is "seed = rand()" killing the given seed. Credit: Jan Weitzel * if a seed is not provided, we use the default rand() values, which produces the same sequence of values every run.r To solve these problems, we seed the random number generator with either the time() or the supplied seed. Cc: Jan Weitzel Signed-off-by: Brian Norris Acked-by: Jan Weitzel --- nandtest.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-) diff --git a/nandtest.c b/nandtest.c index dc28d09..7811be2 100644 --- a/nandtest.c +++ b/nandtest.c @@ -140,6 +140,8 @@ int main(int argc, char **argv) uint32_t offset = 0; uint32_t length = -1; + seed = time(NULL); + for (;;) { static const char *short_options="hkl:mo:p:s:"; static const struct option long_options[] = { @@ -243,6 +245,8 @@ int main(int argc, char **argv) printf("Bad blocks : %d\n", oldstats.badblocks); printf("BBT blocks : %d\n", oldstats.bbtblocks); + srand(seed); + for (pass = 0; pass < nr_passes; pass++) { loff_t test_ofs;