Randomness Simplified
The Math.random() method returns a pseudorandom number between 0 and 1.
That, like the office of the American vice president, is pretty close to
being useful, but not quite.
They call it a pseudorandom number because an algorithm generates the
numbers, which means it's predictable. Given the right conditions, you
can get the same sequence of pseudorandom numbers from Math.random(). To
get truly random values, you need a hardware device observing something
well and truly unpredictable, such as static in the radio frequencies.
Most computers don't have such random-number generation devices, so
pseudorandom numbers have to do for us.
Math.random() will, however, generate a pseudorandom number between 0
and 1, which takes us about halfway to a useful number. Here's a
solution that returns a random number between any upper and lower limits
you care to send to it:
function returnRandom(lower, upper) {
var range = upper-lower+1;
var l = ("" + range).length;
var randomNumber = (Math.floor(Math.random() * Math.pow(10,l)) %
range) + parseInt(lower);
return randomNumber;
}
Remember that Math.pow() returns the value of its first parameter raised
to the power of its second parameter. Math.pow(3,2) would indicate three
squared, or nine.
So, we're finding in this algorithm the number of "places" in the number
of potential responses we're choosing from. If we need a number between
1 and 100, for example, the number of places is three. We then raise 10
to that power, multiply it by the result of Math.random(), round that
down, and mod it by the difference between the upper and lower values.
Add the lower limit to that, and you have a random number within your
parameters.
» posted by ITworld staff
ITworld
Symantec Backup Exec 12 and Backup Exec System Recovery 8 deliver industry leading Windows data protection and system recovery. Download this whitepaper to find out the top reasons to upgrade and how to get continuous data protection and complete system recovery.
Data and system loss — from a hard drive failure, malicious attack, natural disaster, or simple human error — can happen anytime. Don’t leave your business vulnerable. Make sure you have a secure recovery strategy in place. Symantec's latest backup and system recovery technology can efficiently restore critical applications, individual emails and documents and even restore your entire system in minutes in the event of a loss.
Businesses face a growing challenge to ensure that the IT environment is properly protected. Backup Exec 12 integrates with other applications in the Symantec family of products, to complement your current data protection strategy, keep your data securely backed up and make it recoverable when you need it most.







