Widget Random Number Generator Notes

/ 22 July 2007

In the most recent release of my Blackjack widget (v1.2) I fixed a serious bug that was visible in the widget. That bug was that the same pattern was used over and over again when shuffling the deck. The bug wasn’t a direct issue with my code but rather a problem with the way that Apple has it’s support for Javascript set up. The original line of code that I used was the normal (for online) way of receiving a random number. That code is: “var random = Math.random()”. It gets the number from a part of the Javascript engine that keeps up the random number generation forever. For Dashboard widgets that part of the Javascript engine gets restarted each time the widget is loaded making it an imposible command to use for random number generation. After a bit of hunting around online I found another way to receive a random number that is from the command-line and therefore is very random (but also only useable in widgets). The code for that is: “var random = parseFloat(“0.” + widget.system(“jot -r 5 | rs -g 0 6 | sed ‘s/ //g’”, null).outputString)” I then use the local variable “random” to continue with the random function as normal. Just make sure that your widget has command-line access, change that in it’s Info.plist file. This workaround is a functional one, but it would be better if the normal Javascript function would work in widgets. I do hope this may help someone later who was/ is having the same bug as my Blackjack widget had. Enjoy, Alex.

Discussion