<?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 in Flex</title>
	<atom:link href="http://www.flex-blog.com/random-number-in-flex/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.flex-blog.com/random-number-in-flex/</link>
	<description>Examples, News and Other Stuff</description>
	<lastBuildDate>Thu, 19 Jan 2012 14:01:31 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: max</title>
		<link>http://www.flex-blog.com/random-number-in-flex/comment-page-1/#comment-1574</link>
		<dc:creator>max</dc:creator>
		<pubDate>Fri, 06 Aug 2010 09:01:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.flex-blog.com/?p=853#comment-1574</guid>
		<description>Your code for generating random integer number between 0 and a number different from 1 is not entirely correct. For example we want to generate an integer random number in the interal [0, 2]. Following yor example the probabilities for getting a 0 and a 1 are not equal. To get 0 (Math.random() * end) has to return a number in the interval [0, 0.5) so when rounded it will return 0. But to get 1, the number generated by (Math.random() * end) has to be in the interval [0.5, 1.5) which as you can see is two times bigger that the previous one. So the probability to get 1 compared to 0 is bigger. The same happens at the end of our wanted interval (in this example 2).
The easiest way to get equal probability is with this function:

private function rand(start:Number, end:Number):Number
{
	return Math.ceil(Math.random()*(end-start+1)) % (end-start+1);
}

Note that this function too doesn&#039;t generate numbers with mathematically equal probability but it&#039;s close enough.
Note2: This function also work if you pass it start=0, end=1

To test that your example is not correct you can try this example:




	
		&lt;![CDATA[
			
			private function rand(start:Number, end:Number):Number
			{
				return Math.ceil(Math.random()*(end-start+1)) % (end-start+1);
			}
			
			
			private function test_rand():void
			{
				var tmp:Array = new Array(0, 0, 0);
				var i:Number;
				var count:Number = 1000000;
				for( i = 0; i &lt; count; i++ ) {
					//tmp[rand(0, 2)]++;
					tmp[Math.round(Math.random()*2)]++;
				}
				
				lbl_test_rand.text = &quot;&quot;;
				for( i = 0; i 
	
	
	
	


With your algorithm you will get probabilities such as:
0 -&gt; 25%
1 -&gt; 50%
2 -&gt; 25%

with the function rand() (uncomment it in the loop and comment out the line below it) you will get probabilities such as:

0 -&gt; 33%
1 -&gt; 33%
2 -&gt; 33%</description>
		<content:encoded><![CDATA[<p>Your code for generating random integer number between 0 and a number different from 1 is not entirely correct. For example we want to generate an integer random number in the interal [0, 2]. Following yor example the probabilities for getting a 0 and a 1 are not equal. To get 0 (Math.random() * end) has to return a number in the interval [0, 0.5) so when rounded it will return 0. But to get 1, the number generated by (Math.random() * end) has to be in the interval [0.5, 1.5) which as you can see is two times bigger that the previous one. So the probability to get 1 compared to 0 is bigger. The same happens at the end of our wanted interval (in this example 2).<br />
The easiest way to get equal probability is with this function:</p>
<p>private function rand(start:Number, end:Number):Number<br />
{<br />
	return Math.ceil(Math.random()*(end-start+1)) % (end-start+1);<br />
}</p>
<p>Note that this function too doesn't generate numbers with mathematically equal probability but it's close enough.<br />
Note2: This function also work if you pass it start=0, end=1</p>
<p>To test that your example is not correct you can try this example:</p>
<p>		&lt;![CDATA[</p>
<p>			private function rand(start:Number, end:Number):Number<br />
			{<br />
				return Math.ceil(Math.random()*(end-start+1)) % (end-start+1);<br />
			}</p>
<p>			private function test_rand():void<br />
			{<br />
				var tmp:Array = new Array(0, 0, 0);<br />
				var i:Number;<br />
				var count:Number = 1000000;<br />
				for( i = 0; i &lt; count; i++ ) {<br />
					//tmp[rand(0, 2)]++;<br />
					tmp[Math.round(Math.random()*2)]++;<br />
				}</p>
<p>				lbl_test_rand.text = &quot;&quot;;<br />
				for( i = 0; i </p>
<p>With your algorithm you will get probabilities such as:<br />
0 -&gt; 25%<br />
1 -&gt; 50%<br />
2 -&gt; 25%</p>
<p>with the function rand() (uncomment it in the loop and comment out the line below it) you will get probabilities such as:</p>
<p>0 -&gt; 33%<br />
1 -&gt; 33%<br />
2 -&gt; 33%</p>
]]></content:encoded>
	</item>
</channel>
</rss>

