Re: rand() and srand() broken?
site_archiver@lists.apple.com Delivered-To: darwin-dev@lists.apple.com Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=3rGbA3vuWDxeDQy/V/LZEJjMrpOHUEp/fvcNQLL/998=; b=YacLSp7MD3LHVwMnSrtf7I4qUNp8dkua8/BxSpgYf9vVHKx1nkxW18uMovplTFc0D+ 8P+aWkZUYN4ifj8F3UGkA39I8316Ev38lOu/o7YzAp70+ptM+jElX9mqxchqyGuHUQkL vfCNRWXHF/u5R5D/m9ZpI3dxvoBhw5jYWCAYs= Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=JYn4HDMszuCgEB3ys4kPuTg7AkjG6ISMvBT/NwpwT9r/zbX6az8kbdATz3YZq/R08F 7s3yY+jOzpYgLYCwMtucYTZ701Ef4L4GtAf2WQQi6qvFlz51vGTKi5M/cb3d1k8zmo+o LzkCMFm0/7oj+YE73d6KEeb3caWDuPqhXJFuk= On Wed, Nov 25, 2009 at 2:47 PM, Derek Gaston <friedmud@gmail.com> wrote:
On Nov 25, 2009, at 12:41 PM, Clark Cox wrote:
Yes. If you pass the same value to seed(), you will get the same sequence of values from rand(). Period. Any implementation of srand/rand that does not do this is broken. From the C Standard:
Indeed... note that this wasn't about passing the _same_ value... it was about passing similar values to srand... like singularly increasing integers...
I was responding specifically to: "Are you (and everyone else that replied) trying to tell me that the first number out of rand() (after reseeding) should always be the predictable?" And, the answer is yes. The first number out of rand (as well as every subsequent number for that matter) is 100% predictable after re-seeding. By definition, the value that comes out of rand is directly related to the value passed into srand, because it is a PRNG. You will never get truly random numbers out of rand. The only way that you will even get close is to seed it with a single, hard-to-predict value (the time, a random number from a true RNG, etc.), *any* other use of rand will not guarantee you numbers that are anywhere near random. -- Clark S. Cox III clarkcox3@gmail.com _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-dev mailing list (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/site_archiver%40lists.appl... This email sent to site_archiver@lists.apple.com
participants (1)
-
Clark Cox