I started by writing a function to pick a random number between two numbers given as arguments.
function get_random( $max, $min)
{
// return a random number between $min and $max
$diff=$max-$min
return (Math.floor((Math.random() % 1) * $diff) + $min);
}
Most of the work here is done by functions from the JavaScript math library, which we use to pick a number between zero and the difference between our two numbers. Then we add the minimum number to this value so that the result is between our two numbers. (If you aren't familiar with random numbers, you might want to check out this article.)
Then I simply called this function with appropriate arguments (120 and 30) in my do_alarm function where I had previously just defined a number to use as seconds to wait.
function do_alarm(){
// mark alarm as set
document.getElementById("alarm_button").disabled=true
document.getElementById("alarm_button").value="Alarm Set"
// determine time to wait in seconds
wait_secs=get_random(120, 30)
// wait for time to be up & sound alarm
setTimeout("sound_alarm()", (wait_secs * 1000))
}
Now the program alarm is set to a random interval, although it otherwise works exactly the same as my last revision. You can view all the code and try out a working example here.