AS3 randRange() function

Recently I needed to generate a random number of items for a unit test in flex. In ColdFusion this is easy, I can use the randRange(x,x) function. However, there is not a randrange() function built into AS3.

So with a little help from google, here is a simple AS3 randRange() you can use.

[Update: Thanks to Till & Keith, here is an updated function]

function randRange(start:Number, end:Number) : Number
{
return Math.floor(start +(Math.random() * (end - start)));
}

Comments
Keith Peters's Gravatar I don't think that's quite right. That'll give you a number from start to start+end

probably more like:

return Math.round(start +(Math.random() * (end - start)));
# Posted By Keith Peters | 10/3/06 10:33 PM
Till Schneidereit's Gravatar And even the version Keith posted will give you statistically skewed values - altough in the correct range. The problem is, that the first and the last value are exactly half as likely to be hit than the other ones. The reason being that the lowest value V[0] is only hit if the un-rounded value is between V[0] and V[0].499999[and so on] and the highest value Vn is only hit if the un-rounded value is between V[n-1].5 and V[n], while all other values V[x] are hit if the un-rounded value is between V[x-1].5 and V[x].499999[and so on].
In case my explanation doesn't make any sense, here's some code to prove it: (I wrote this code in the Flash IDE, so you'll have to turn it into a class to use it in Flex)

function randRange (start:Number, end:Number) : Number
{
   return Math.round(start +(Math.random() * (end - start)));
}
var START:Number = 10;
var END:Number = 20;
var results:Array = [];
var i:Number;

//initialize the results array
for(i = START; i < END + 1; i++)
{
   results[i] = 0;
}

//calculate lots of random values between START and END and increase the corresponding results index
for(i = 0; i < 50000; i++)
{
   var index:Number = randRange(START, END);
   results[index] = results[index] + 1;
}

//print the results
for(i = START; i < END + 1; i++)
{
   trace(i + ": "+results[i]);
}


To create statistically sound values, you need to use a function similar to this one:

function randRange2 (start:Number, end:Number) : Number
{
   return Math.floor(start +(Math.random() * (end - start + 1)));
}


While this version does exactly what you seem to want to do, I'm still not sure if it really works identically to the one in ColdFusion. Normally, functions like this one should return values *between* the two values, meaning (value >= start && value < end), not (value >= start && value <= end). If you want to have your function work identically to that in ColdFusion, you should check theirs and, based on your results, maybe change your function to:

function randRange3 (start:Number, end:Number) : Number
{
   return Math.floor(start +(Math.random() * (end - start)));
}


cheers,
till
# Posted By Till Schneidereit | 10/4/06 6:20 PM
Mike Nimer's Gravatar Till,
thanks! It's funny what I thought was a simple function turns out to be almost a simple function. I'm not looking for the exact same behavior as CF, I was just using it as a reference since it does have this ability.

I'm going to update the blog and my util function with your function, since it is more accurate.

thanks,
---nimer
# Posted By Mike Nimer | 10/4/06 6:53 PM
????'s Gravatar Thank you for your reliable post, that?s what i was searching for.
# Posted By ???? | 10/7/07 4:37 AM
cc's Gravatar Thanks for letting me know, it was those pesky Word ' and " characters. It's all set now.
# Posted By cc | 4/14/08 12:59 AM
dfsfdg's Gravatar <a href="http://umanity.jp/"; REL="nofollow">??</a>   ?
<a href="http://umanity.jp/"; REL="nofollow">????</a>   ?
<a href="http://www.seo-by.jp/"; REL="nofollow">????SEO</a>   ??

<a href="http://www.seo-by.jp/"; REL="nofollow">??SEO</a>   ??????????????
<a href="http://www.kabu-net.com/"; REL="nofollow">????</a>   ??
<a href="http://www.kabu-net.com/"; REL="nofollow">?? ??</a>   ????????????????

<a href="http://www.j-payment.co.jp/service/marketing/seo.h...; REL="nofollow">SEO</a>   ?
<a href="http://www.j-payment.co.jp/service/marketing/seo.h...; REL="nofollow">SEO??</a>   ????????
<a href="http://www.seo-by.jp/"; REL="nofollow">SEO</a>   ???????

<a href="http://www.seo-by.jp/"; REL="nofollow">????? ??</a>   ?
<a href="http://www.seo-by.jp/"; REL="nofollow">??????? ??</a>   ????
<a href="http://www.seo-by.jp/"; REL="nofollow">???????? ??</a>   ???????

<A HREF="http://office-eiki.com/"; REL="nofollow">????</A>   ?
<A HREF="http://yyamada-law.com/"; REL="nofollow">????</A>   ??????????
<A HREF="http://saimuseiri-ing.com/"; REL="nofollow">????</A>   ?

<A HREF="http://www.kajimitsuo.net/"; REL="nofollow">????</A>   ?????
<A HREF="http://www.mercywork.net/"; REL="nofollow">??? ?????</A>   ?????
<A HREF="http://www.mercywork.net/navi/m/index.html"; REL="nofollow">??? ?????</A>   ?????
<A HREF="http://www.yumeki.com/index.html"; REL="nofollow">???????????</A>   ??
<A HREF="http://www.yumeki.com/"; REL="nofollow">???????????</A>   ??OK

<a href="http://www.web-tantei.com/">??</a>;   ?
<a href="http://www.web-tantei.com/">?????</a>...;   ?
<a href="http://www.web-tantei.com/">?????</a>...;   ?
<a href="http://www.web-tantei.com/">????????</a...;   ?
<a href="http://web-tantei.com/">???</a>;   
   ?
<a href="http://www.web-tantei.com/">???</a>;   ?????
<a href="http://www.akasaka-j.com/">????</a>;   ?
<a href="http://www.akasaka-j.com/">?????</a>;   ?
<a href="http://www.akasaka-j.com/">????</a>;   ????????
<a href="http://www.akasaka-j.com/">???</a>;   
   ?
<a href="http://www.akasaka-argus.com/">????</a&...;   ?????
<a href="http://www.akasaka-argus.com/">?????</a...;   ?
<a href="http://www.akasaka-argus.com/">???</a&g...;   ????????
<a href="http://www.akasaka-argus.com/">????</a&...;
# Posted By dfsfdg | 8/16/08 3:53 AM
adfe's Gravatar <a href="http://conveni.payment-by.jp/">??????</...;
<a href="http://conveni.payment-by.jp/">????</a&...;
<a href="http://conveni.payment-by.jp/">?????????&l...;
<a href="http://conveni.payment-by.jp/">??????</...;
<a href="http://conveni.payment-by.jp/">??????????&...;
<a href="http://conveni.payment-by.jp/">??????????&...;
<a href="http://conveni.payment-by.jp/">???????????...;

<a href="http://www.j-payment.co.jp/service/electronic/inde...;
<a href="http://www.j-payment.co.jp/service/electronic/inde...;
<a href="http://www.j-payment.co.jp/service/electronic/inde...;
<a href="http://www.j-payment.co.jp/service/electronic/inde...??</a>" target="_blank">http://www.j-payment.co.jp/service/electronic/inde...;
<a href="http://www.j-payment.co.jp/service/electronic/inde...;
<a href="http://www.j-payment.co.jp/service/electronic/inde...???? </a>

<a href="http://webmoney.payment-by.jp/">?????</...;
<a href="http://webmoney.payment-by.jp/">???????<...;
<a href="http://webmoney.payment-by.jp/">????????&l...;
<a href="http://webmoney.payment-by.jp/">???money??...;
<a href="http://webmoney.payment-by.jp/">??????????...;
<a href="http://webmoney.payment-by.jp/">???money??...;

<a href="http://www.j-payment.co.jp/service/banktransferset...;
<a href="http://www.j-payment.co.jp/service/banktransferset...;
<a href="http://www.j-payment.co.jp/service/banktransferset...;
<a href="http://www.j-payment.co.jp/service/banktransferset...;
<a href="http://www.j-payment.co.jp/service/banktransferset...;
<a href="http://www.j-payment.co.jp/service/banktransferset...;
<a href="http://www.j-payment.co.jp/service/banktransferset...;
<a href="http://www.j-payment.co.jp/service/banktransferset...;
<a href="http://www.j-payment.co.jp/service/banktransferset...;
<a href="http://www.j-payment.co.jp/service/banktransferset...;
<a href="http://www.j-payment.co.jp/service/banktransferset...;
<a href="http://www.j-payment.co.jp/service/banktransferset...;
# Posted By adfe | 8/16/08 3:54 AM
dfvx's Gravatar <a href="http://www.accessup-by.jp/"; REL="nofollow">????SEO</a>
<a href="http://www.accessup-by.jp/"; REL="nofollow">??SEO</a>
<a href="http://www.accessup-by.jp/"; REL="nofollow">????? ??</a>
<a href="http://www.dh1.jp/"; REL="nofollow">????????</a>
<a href="http://www.dh1.jp/"; REL="nofollow">??? ????????</a>
<a href="http://denwauranai-patio.com/">;????</a>" target="_blank">http://denwauranai-patio.com/">;????</a&...;
<a href="http://denwauranai-patio.com/">;?? ??</a>
<a href="https://www.fes7.co.jp/">;??</a>" target="_blank">https://www.fes7.co.jp/">;??</a>;
<a href="https://www.fes7.co.jp/">;???? ??</a>
<a href="https://www.fes7.co.jp/">;??</a>" target="_blank">https://www.fes7.co.jp/">;??</a>;
<a href="http://www.clarity-b.info/">???????</a&...;
<a href="http://www.clarity-b.info/">??????</a&g...;
<a href="http://www.clarity-b.info/">???????????<...;
<a href="http://www.clarity-b.info/">?????????</...;
<a href="http://www.clarity-b.info/">????????????&l...;
<a href="http://www.clarity-b.info/">??????????????...;
<a href="http://www.clarity-b.info/">?????????????&...;
<a href="http://www.clarity-b.info/">??????????<...;
<a href="http://www.clarity-b.info/">??????????<...;
<a href="http://www.clarity-b.info/">?????????</...;
<a href="http://eigo2.acqwords.com/cd/gengo.php">??...;
<a href="http://eigo2.acqwords.com/cd/gengo.php">??...;
<a href="http://eigo2.acqwords.com/cd/gengo.php">??...;
<a href="http://eigo2.acqwords.com/cd/gengo.php">??...;
<a href="http://eigo2.acqwords.com/cd/gengo.php">??...;
<a href="http://eigo2.acqwords.com/cd/gengo.php">??...;
<a href="http://www.clubwith.tv/?cn=C_MAIN_PC">????...;
<a href="http://www.party55.com/">???????????</a...;
<a href="http://fasciere.jp/">???????</a>;
<a href="http://fasciere.jp/">???????</a>;
<a href="http://fasciere.jp/">???????</a>;
<a href="http://www.feelweb.jp/index.html">????????...;
<a href="http://www.feelweb.jp/index.html">????????...;
# Posted By dfvx | 8/16/08 3:57 AM
thanh99's Gravatar <a href="http://www.seo-by.jp/"; REL="nofollow">????SEO</a>
<a href="http://www.seo-by.jp/"; REL="nofollow">??SEO</a>
<a href="http://www.kabu-net.com/"; REL="nofollow">????</a>
<a href="http://www.kabu-net.com/"; REL="nofollow">?? ??</a>
<a href="http://www.j-payment.co.jp/service/marketing/seo.h...; REL="nofollow">SEO</a>
<a href="http://www.j-payment.co.jp/service/marketing/seo.h...; REL="nofollow">SEO??</a>
<a href="http://www.seo-by.jp/"; REL="nofollow">SEO</a>
<a href="http://www.seo-by.jp/"; REL="nofollow">????? ??</a>
<a href="http://www.seo-by.jp/"; REL="nofollow">??????? ??</a>
<a href="http://www.seo-by.jp/"; REL="nofollow">???????? ??</a>
<A HREF="http://www.mercywork.net/"; REL="nofollow">??? ?????</A>
<A HREF="http://www.mercywork.net/navi/m/index.html"; REL="nofollow">??? ?????</A>
<A HREF="http://www.yumeki.com/index.html"; REL="nofollow">???????????</A>
<A HREF="http://www.yumeki.com/"; REL="nofollow">???????????</A>
<a href="http://web-tantei.com/">???</a>;
<a href="http://www.web-tantei.com/">???</a>;
<a href="http://www.dh1.jp/"; REL="nofollow">????????</a>
<a href="http://www.dh1.jp/"; REL="nofollow">??? ????????</a>
<a href="http://denwauranai-patio.com/">;????</a>" target="_blank">http://denwauranai-patio.com/">;????</a&...;
<a href="http://denwauranai-patio.com/">;?? ??</a>
<a href="https://www.fes7.co.jp/">;??</a>" target="_blank">https://www.fes7.co.jp/">;??</a>;
<a href="https://www.fes7.co.jp/">;???? ??</a>
<a href="https://www.fes7.co.jp/">;??</a>" target="_blank">https://www.fes7.co.jp/">;??</a>;
<a href="http://www.eyecosme.jp/">???????</a>;
<a href="http://www.eyecosme.jp/">????????</a>...;
<a href="http://www.accessup-by.jp/"; REL="nofollow">????SEO</a>
<a href="http://www.clubwith.tv/?cn=C_MAIN_PC">????...;
<a href="http://www.party55.com/">???????????</a...;
<a href="http://www.clubwith.tv/?cn=C_MAIN_PC">????...;
<a href="http://www.party55.com/">????????????</...;
<a href="http://www.accessup-by.jp/"; REL="nofollow">??SEO</a>
<a href="http://www.accessup-by.jp/"; REL="nofollow">????? ??</a>
<a href="http://www.nizikaikun.com/"; >???</a>
<a href="http://www.wac-up.com/">??</a>;
# Posted By thanh99 | 9/2/08 7:49 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.001.