# Random range

**URL:** https://forum.kirupa.com/t/random-range/1693
**Category:** Uncategorized
**Created:** [June 8, 2002, 3:08am UTC](https://forum.kirupa.com/t/random-range/1693 "2002-06-08T03:08:46Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 8, 2002, 3:08am UTC](https://forum.kirupa.com/t/random-range/1693/1 "2002-06-08T03:08:46Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 8, 2002, 3:22am UTC](https://forum.kirupa.com/t/random-range/1693/2 "2002-06-08T03:22:35Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 8, 2002, 3:27am UTC](https://forum.kirupa.com/t/random-range/1693/3 "2002-06-08T03:27:42Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 8, 2002, 11:44am UTC](https://forum.kirupa.com/t/random-range/1693/4 "2002-06-08T11:44:39Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 8, 2002, 11:46pm UTC](https://forum.kirupa.com/t/random-range/1693/5 "2002-06-08T23:46:23Z")

</div>

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]

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 10, 2002, 1:38pm UTC](https://forum.kirupa.com/t/random-range/1693/6 "2002-06-10T13:38:23Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 10, 2002, 2:15pm UTC](https://forum.kirupa.com/t/random-range/1693/7 "2002-06-10T14:15:08Z")

</div>

Gna gna gna 😛

```auto
 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

```

😃 \r\rpom 0]

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 10, 2002, 3:29pm UTC](https://forum.kirupa.com/t/random-range/1693/8 "2002-06-10T15:29:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 10, 2002, 10:29pm UTC](https://forum.kirupa.com/t/random-range/1693/9 "2002-06-10T22:29:42Z")

</div>

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