My solution would use the Math.random too - couldn't find a good alternative. I used this to provide an alternative other Math.* functions: http://bit.ly/TTua1H
//Function similar to range() in PHP - modded from http://bit.ly/MlBd05
Array.range = function(from, to, increment){
var A = [];
if(typeof from == 'number'){
A[0]= from;
increment = increment || 1;
while(from + increment <= to){
A[A.length]= from += increment;
}
}
return A;
}
pool = Array.range(17,24,1); //Array 17,18,19...24
numItems = pool.length;
randomKey = Math.random() * numItems | 0; // this is the bitop
document.write('The array contains: {' + pool.join(', ') + '}<br />');
document.write('Random key: ' + randomKey + ' (0-based) and the value: ' + pool[randomKey] + '<br />');
For testing randomness, you could do soemthing like this:
pool = Array.range(17,24,1); //from the previous code - Array.range...
numItems = pool.length;
counterArray = Array();
for(i=0;i<numItems;i++) counterArray[i] = 0;
document.write('The array contains: {' + pool.join(', ') + '}<br />');
for(i=0;i<1000;i++){
randomKey = Math.random() * numItems | 0;
counterArray[randomKey]++;
//document.write('Random key: ' + randomKey + ' (0-based) and the value: ' + pool[randomKey] + '<br />');
}
document.write('The totals of each key are: {' + counterArray.join(', ') + '}<br />');
diafol
Keep Smiling
10,848 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,539
Skill Endorsements: 61
Lines 18/19 are just to display to the screen
double pipe || is or to set the increment to 1 if it's not set
You can forget the Array.range bit if you want - it was just an easy way to create an array in the first place.
from + increment <= to
Just means - keep doing it until (from + increment) is less or equal to 'to'
The pipe (line 16) is the bit operator - it's a quicker way of doing:
Math.floor or Math.abs as can be seen here: http://bit.ly/TTua1H
diafol
Keep Smiling
10,848 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,539
Skill Endorsements: 61
@Proglearner, Your script is not working...
1)Line 6, j = Math.abs(Math.random() * 10);, the Math.abs() does nothing for you. The value returned from Math.random() is between 0 and 1 (a float), so it is always a positive number anyway.
2)Line 11, oneString=String(oneArray[Math.abs(Math.random() * 10)]); would almost always produce undefined. (Also Math.abs() does nothing for you either.) The reason is that Math.random() returns a float value between 0 and 1 and most likely not to be able to multiply with your selected number and produces an integer (number). An array index is an integer (number) which has no decimal. As a result, you attempt to access a non-existing array index.
// i.e.
var rand = Math.random() * 10;
// let say Math.random() returns 0.23453
// now your rand is 2.3453
// as a result, an array of array[rand] will give you "undefined" value
What you need to do is to use Math.floor() which truncates any decimal produced by the Math.random().
oneString = String(oneArray[Math.floor(Math.random()*10)])
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 376
Skill Endorsements: 17
Question Answered as of 5 Months Ago by
diafol,
Taywin
and
radhakrishna.p