random number generating

• Apr 15, 2012, 13:32
random number generating
how can i create a random number that is in between multiple variables. for example 0-10, 100-110.

it can be in between either of the two options but no greater or less
• Apr 15, 2012, 14:22
mrhoo
Code:

```If you have multiple ranges, put them in an array: var arr=[[0,5],[100,105]]; get a random index of the array: r=arr[Math.floor(Math.random()*arr.length)], min=r[0], max=r[1]; then generate the random number from the selected range. Math.floor(Math.random()*(max-min+1)+min)```
• Apr 19, 2012, 11:52
muazzez
This code is working in Konqueror 4.5.5 and Firefox 4.0b9
Code:

```  <script type="text/javascript"> var A =[ [0,10], [100,110]]; var t = String(new Date().getTime()).slice(this.length-2); alert(t); var r = A[Math.floor(Math.random()*A.length)]; var min=r[0]; var max=r[1]; var mod = max - min + 1; var kalan = Number(t) % mod; var result = min + kalan; alert(result); </script>```
• Apr 19, 2012, 12:38
muazzez
without using Math.random
Code:

```  <script type="text/javascript"> var A =[ [0,10], [100,110]]; var s = String(new Date().getTime()); var d = s.slice(this.length-1); alert(d); var t=  s.slice(this.length-2); alert(t); var r = A[ Number(d) % A.length]; alert(r); var min=r[0]; var max=r[1]; var mod = max - min + 1; var kalan = Number(t) % mod; var result = min + kalan; alert(result); </script>```