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
| SitePoint Sponsor |
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




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)


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>
The Time Through Ages. In the Name of Allah, Most Gracious, Most
Merciful.1.By the Time, 2.Verily Man is in loss, 3. Except such
as have Faith, and do righteous deeds, and (join together) in the
mutual enjoining of Truth, and of Patience and Constancy.


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>
The Time Through Ages. In the Name of Allah, Most Gracious, Most
Merciful.1.By the Time, 2.Verily Man is in loss, 3. Except such
as have Faith, and do righteous deeds, and (join together) in the
mutual enjoining of Truth, and of Patience and Constancy.
Bookmarks