Sep 3, 2012 - javascript, 前端    No Comments

Javascript Random

前几天弄了一下node.js的uuid,发现这个js其实也是可以用在前端的。同时又在项目中用到了那个getSeconds();的简单随机数。于是有了整理一下javascript随机数的念头。顺便写了一篇教程给公司的实习生用。

简单的getSeconds();随机数:

var randomNumberbySeconds = function(maxnum) {
	var now = new Date();
	//var number = now.getSeconds(); // 0 - 59 随机数
	//var number = now.getSeconds() % 43; // 0 - 42
	//var number = now.getSeconds() % (maxnum + 1);  // 0 ~ 43
	var number = now.getSeconds() % maxnum + 1; // 1 ~ 43
	return number;
}


时间随机数升级版:

var randomrandom = function(number) {
	randomrandom.today = new Date();
	randomrandom.seed = randomrandom.today.getTime();
	randomrandom.seed = (randomrandom.seed * 9301 + 49297) % 233280;
	randomrandom.sedd = randomrandom.seed / (233280.0);
	return Math.ceil(randomrandom.seed * number);
}

在线看完整的例子:看这里看这里看这里

下载完整的例子:点这里点这里点这里

Got anything to say? Go ahead and leave a comment!