xbolt
Medic
- Joined
- Apr 23, 2008
- Messages
- 7
- Reaction score
- 3
The quote rotation script at the top of the page has a logic error, making it so the first and last entries in the array come up less often than the rest.
This part right here:
var whichQuotation=Math.round(Math.random()*(Q-1));
It uses Math.round, which just rounds to the nearest integer. This works, but the first entry in the array only comes up if the generated number is between 0 and 0.5. That's a range of only 0.5. The second entry in the array comes up if the generated number is between 0.5 and 1.5, a range of 1. Now, the last entry only comes up if the number generated is between 2.5 and 3, making the range 0.5 again.
Here is corrected code:
var whichQuotation=Math.floor(Math.random()*(Q));
It looks similar, but uses Math.floor instead, so it only rounds down. So everything between 0 and 1 becomes 0, everything between 1 and 2 becomes 1, and so on. This makes it so all the entries have the same chance of showing up.
This part right here:
var whichQuotation=Math.round(Math.random()*(Q-1));
It uses Math.round, which just rounds to the nearest integer. This works, but the first entry in the array only comes up if the generated number is between 0 and 0.5. That's a range of only 0.5. The second entry in the array comes up if the generated number is between 0.5 and 1.5, a range of 1. Now, the last entry only comes up if the number generated is between 2.5 and 3, making the range 0.5 again.
Here is corrected code:
var whichQuotation=Math.floor(Math.random()*(Q));
It looks similar, but uses Math.floor instead, so it only rounds down. So everything between 0 and 1 becomes 0, everything between 1 and 2 becomes 1, and so on. This makes it so all the entries have the same chance of showing up.