<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Random Number Between Two Integers</title>
	<atom:link href="http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html/feed" rel="self" type="application/rss+xml" />
	<link>http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html</link>
	<description>Making Your Code Faster, Stronger, Safer…</description>
	<lastBuildDate>Tue, 31 Aug 2010 16:47:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: C++ problems - Programming &#124; TechEnclave</title>
		<link>http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3484</link>
		<dc:creator>C++ problems - Programming &#124; TechEnclave</dc:creator>
		<pubDate>Sat, 06 Feb 2010 19:54:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3484</guid>
		<description>[...] if you want to learn a bit more than just getting your question answered, take a look at this: Random Number Between Two Integers &#124; Safer Code - Secure Coding In C C++ And More.. (Also read the comments which are quite [...]</description>
		<content:encoded><![CDATA[<p>[...] if you want to learn a bit more than just getting your question answered, take a look at this: Random Number Between Two Integers | Safer Code &#8211; Secure Coding In C C++ And More.. (Also read the comments which are quite [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: -</title>
		<link>http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3470</link>
		<dc:creator>-</dc:creator>
		<pubDate>Tue, 05 Jan 2010 08:26:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3470</guid>
		<description>Paul, 

There is the low order bit issue with rand() in many implemenations (although in many instances they have been deprecated and replaced by better implementation.... but generally it is better to assume SNAFU than assume things are good.).

But also:

j = 1 + (int) (10.0 * (rand() / (RAND_MAX + 1.0)));

will work well even when the size factor (in this case 10.0) approaches RAND_MAX. 

Compare to modulus which will introduce bias when the size factor approaches RAND_MAX (which Shivan pointed out). Modulus loose the uniform randomness produced by rand(). I.e. modulus is logically broken regardless of rand() implementation. 

If someone figured out how to do it right allready in 1992, why forget it in 2010? Don&#039;t reintroduce old programming sins :-)</description>
		<content:encoded><![CDATA[<p>Paul, </p>
<p>There is the low order bit issue with rand() in many implemenations (although in many instances they have been deprecated and replaced by better implementation&#8230;. but generally it is better to assume SNAFU than assume things are good.).</p>
<p>But also:</p>
<p>j = 1 + (int) (10.0 * (rand() / (RAND_MAX + 1.0)));</p>
<p>will work well even when the size factor (in this case 10.0) approaches RAND_MAX. </p>
<p>Compare to modulus which will introduce bias when the size factor approaches RAND_MAX (which Shivan pointed out). Modulus loose the uniform randomness produced by rand(). I.e. modulus is logically broken regardless of rand() implementation. </p>
<p>If someone figured out how to do it right allready in 1992, why forget it in 2010? Don&#8217;t reintroduce old programming sins <img src='http://www.safercode.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul Leopardi</title>
		<link>http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3469</link>
		<dc:creator>Paul Leopardi</dc:creator>
		<pubDate>Mon, 04 Jan 2010 23:44:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3469</guid>
		<description>The previous comment assumes that the high-order bits are of better quality than the lower-order bits. This is true for some older pseudo-random number generators. The GNU Scientific Library provides a good selection of better generators, including Mersenne Twister. See also http://guru.multimedia.cx/pseudo-random-number-generators/</description>
		<content:encoded><![CDATA[<p>The previous comment assumes that the high-order bits are of better quality than the lower-order bits. This is true for some older pseudo-random number generators. The GNU Scientific Library provides a good selection of better generators, including Mersenne Twister. See also <a href="http://guru.multimedia.cx/pseudo-random-number-generators/" rel="nofollow">http://guru.multimedia.cx/pseudo-random-number-generators/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: -</title>
		<link>http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3468</link>
		<dc:creator>-</dc:creator>
		<pubDate>Mon, 04 Jan 2010 13:52:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3468</guid>
		<description>In Numerical Recipes in C: The Art of Scientific Computing (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; New York: Cambridge University Press, 1992 (2nd ed., p. 277)), the following comments are made:

&quot;If you want to generate a random integer between 1 and 10, you should always do it by using high-order bits, as in
j = 1 + (int) (10.0 * (rand() / (RAND_MAX + 1.0)));

and never by anything resembling
j = 1 + (rand() % 10);

(which uses lower-order bits).&quot;</description>
		<content:encoded><![CDATA[<p>In Numerical Recipes in C: The Art of Scientific Computing (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; New York: Cambridge University Press, 1992 (2nd ed., p. 277)), the following comments are made:</p>
<p>&#8220;If you want to generate a random integer between 1 and 10, you should always do it by using high-order bits, as in<br />
j = 1 + (int) (10.0 * (rand() / (RAND_MAX + 1.0)));</p>
<p>and never by anything resembling<br />
j = 1 + (rand() % 10);</p>
<p>(which uses lower-order bits).&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nobody</title>
		<link>http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3464</link>
		<dc:creator>nobody</dc:creator>
		<pubDate>Wed, 30 Dec 2009 10:55:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3464</guid>
		<description>What&#039;s wrong with rand()*(b-a) + a for 0&lt;rand&lt;1 and relatively small b,a ?</description>
		<content:encoded><![CDATA[<p>What&#8217;s wrong with rand()*(b-a) + a for 0&lt;rand&lt;1 and relatively small b,a ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anon</title>
		<link>http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3463</link>
		<dc:creator>Anon</dc:creator>
		<pubDate>Wed, 30 Dec 2009 04:12:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3463</guid>
		<description>rand() is a bad function for random computation. Look up the Mersenne Twister algoirthm implemented in I think the TR1 spec? Or alternately look up &quot;Press&quot; numerical methods book.</description>
		<content:encoded><![CDATA[<p>rand() is a bad function for random computation. Look up the Mersenne Twister algoirthm implemented in I think the TR1 spec? Or alternately look up &#8220;Press&#8221; numerical methods book.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Links 29/12/2009: GNU/Linux for Kerala Legislators, Monty Still Rushes Against Oracle &#124; Boycott Novell</title>
		<link>http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3462</link>
		<dc:creator>Links 29/12/2009: GNU/Linux for Kerala Legislators, Monty Still Rushes Against Oracle &#124; Boycott Novell</dc:creator>
		<pubDate>Wed, 30 Dec 2009 02:08:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3462</guid>
		<description>[...] Random Number Between Two Integers [...]</description>
		<content:encoded><![CDATA[<p>[...] Random Number Between Two Integers [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3460</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Tue, 29 Dec 2009 15:22:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3460</guid>
		<description>Thanks for the inputs, Shivan, Paul and passer-by. I was aware of the slight bias that this would cause and didn&#039;t suggest an iterative approach because of its performance demerits but the last one suggested by passer-by seems interesting. Will try this out and update. Thanks once again for your views, keep em coming :)</description>
		<content:encoded><![CDATA[<p>Thanks for the inputs, Shivan, Paul and passer-by. I was aware of the slight bias that this would cause and didn&#8217;t suggest an iterative approach because of its performance demerits but the last one suggested by passer-by seems interesting. Will try this out and update. Thanks once again for your views, keep em coming <img src='http://www.safercode.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Passer-by</title>
		<link>http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3459</link>
		<dc:creator>Passer-by</dc:creator>
		<pubDate>Tue, 29 Dec 2009 14:36:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3459</guid>
		<description>The only way I know of to get a proper random number in a range 0...N is to first get the full-precision, evenly distributed random number, than AND it with a 1111...-bitmask that is just able to cover N. And then if the result is greater than the max, you throw away the number and try again.

In worst case (for example range 0...128, which must be ANDed with binary 11111111 = 255) you have to throw away just under half of the random numbers you generate. For most purposes, this is good enough performance.

Also, to minimize performance penalty, you can treat the full-precision random numbers not as numbers, but as stream of bits, and then take just enough bits at a time to cover N, and getting more full-precision numbers to fill your bit stream only when needed you run out of bits. This can be a big save if N is much smaller than maximum full-precision random number.</description>
		<content:encoded><![CDATA[<p>The only way I know of to get a proper random number in a range 0&#8230;N is to first get the full-precision, evenly distributed random number, than AND it with a 1111&#8230;-bitmask that is just able to cover N. And then if the result is greater than the max, you throw away the number and try again.</p>
<p>In worst case (for example range 0&#8230;128, which must be ANDed with binary 11111111 = 255) you have to throw away just under half of the random numbers you generate. For most purposes, this is good enough performance.</p>
<p>Also, to minimize performance penalty, you can treat the full-precision random numbers not as numbers, but as stream of bits, and then take just enough bits at a time to cover N, and getting more full-precision numbers to fill your bit stream only when needed you run out of bits. This can be a big save if N is much smaller than maximum full-precision random number.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shivan</title>
		<link>http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3458</link>
		<dc:creator>shivan</dc:creator>
		<pubDate>Tue, 29 Dec 2009 14:15:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/2009/12/08/random-number-between-two-integers.html#comment-3458</guid>
		<description>@Paul, do we have a case of hivemind here ? ;-)</description>
		<content:encoded><![CDATA[<p>@Paul, do we have a case of hivemind here ? <img src='http://www.safercode.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
