Random range

I know that setting a random number like\r\rrandom (5);\r\rwill make the computer choose from 1-4,\r\rbut what if I want to say have a range of\r\r5-10?\r\rHow should the random code look like? Thanks again.

Hi there again… anyway, I found a way\r\r_root.gtout = random (10) + 20\r\rso that I’ll get a random number from 20 to 30… \r\rbut isn’t there a shorter way? hehe…

well, considering that random() has been deprecated, I would say there is a newer, longer way! Yeah!\r\rJust for informational purposes… it goes a little something like this:\r\rMath.floor(Math.random()*10+10);\r\rFun, huh?

random(5) gives values of 0-4 not 1-4

Hey Neongold, how shorter can it be ?\rIf you really want something shorter, you’ll have to create your own function… Not so difficult but useless if I may say so.\r\rpom 0]

here you go:\rfunction fRandom(nRange,nMin){\rreturn ((Math.random()*nRange)+nMin);\r}\r:) \rjeremy

Gna gna gna :stuck_out_tongue:

 function fRandom2 (nMin,nMax){\r\r  return (Math.round(Math.random()*(nMax-nMin))+nMin);\r\r}\r\r\r\rThen test it with :\r\rfor (i=0;i<50;i++) {trace (fRandom2(0,10));}\r\r

:smiley: \r\rpom 0]

or, if you would like to limit within a range and have a bias towards a certain number. in this case i made it an integer, but it doesn’t have to be. bias must be between the min and max for this one:\rfunction fRandom3 (nMin,nMax,nBias){\rif(nMedian>nMin && nMedian<nMax){\rnTemp = (Math.random()(nMax-nMin))+nMin;\rtrace (Math.floor((nTemp+nBias)/2));\r} else {\rtrace (“bias out of range”);\r}\r}\r\rbias can be any number with this one:\rfunction fRandom4 (nMin,nMax,nBias){\rnTemp = (Math.random()(nMax-nMin))+nMin;\rtrace (Math.floor((nTemp+nBias)/2));\r}\r\r\r:) \rjeremy

:smiley: OK, OK, you win…\rBut you changed nBias into nMedian at some point in your script, Sinf.\r\rpom 0]