better and leaner javascript for randomization