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 :content-transfer-encoding; bh=+CF8VbtHnu9RRA2EkOWizAbXRSOgZVC5cbSIQ+DuSis=; b=mObo35TwK+nbdTmB9TuoK59cSt2P8IFE/sLJBfQ8tNtXyrAXCI8n2e8eQhvr5z10+M eGX5NscKLC3ob2u4vmn6u84OVTTjOPNOiofRh0YogKutnYLCmC5xlcgpyItlY8hha650 kzF1MAB4UOvoFe5tDRsgZnccz69f9l5wAjP1Q= 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:content-transfer-encoding; b=YDudICJ8UCMhp77tGNmcTQOv+WCb8HLWJpVTSx7OOi3tRYOB+3HP6cXpQebATPdRmx 89JnolOKouq2XL9wH23Gc0cDLPPlxrKVCpqlZMXE4O/HCf3SlOAFjQdc67pY3BNtEDvl Do6DuGewKRa4qY/jlRMNA+861o1BSCRxZkI0k= On Wed, Nov 25, 2009 at 1:40 PM, Derek Gaston <friedmud@gmail.com> wrote:
On Nov 25, 2009, at 10:47 AM, Jason Foreman wrote:
On Nov 25, 2009, at 11:26 AM, Derek Gaston wrote:
This piece of code should generate 100 random numbers between 0 and 1... I am expecting that they be fairly spaced out as well...
What basis do you have for this expectation? Is it spelled out in any specification or standard somewhere?
You're seeding the RNG with a monotonically increasing seed, and getting what looks like monotonically increasing starting values for your random sequences. The fact that you get what you expect on Linux seems to be purely coincidental.
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?
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: "The srand function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand. If srand is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated." -- 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