当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript 随机数 与高级应用 附vbscript(asp) 随机数总结

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 javascript 随机数 与高级应用 附vbscript(asp) 随机数总结


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 219 ::
收藏到网摘: n/a

有时忘了程序的随机数函数或javascript和vbscript的随机数混乱了,特总结下
两者的随机数函数,以备以后使用方便。
一、是javascript 随机数函数Math.random()
生成指定范围的随机数
Math.random()方法没有参数,返回0~1之间的随机数,如果要生成0~n之间的随机
数,可以使用下面的格式:
Math.floor(Math.random()*n)
若要生成m~n之间的随机数,可以用:
Math.floor(Math.random()*n)+m
下面是运用:
点击运行可以看到效果:
[Ctrl+A 全选 提示:你可先修改部分代码,再按运行]

javascript 随机数 高级应用
①自JavaScript产生后,好多浏览器中都有内置的随机数发生方法。例如:
var number = Math.random();
该方法产生一个0到1之间的浮点数。
②基于时间,亦可以产生随机数。例如:
var now=new Date();
var number = now.getSeconds();
这将产生一个基于目前时间的0到59的整数。
var now=new Date();
var number = now.getSeconds()%43;
这将产生一个基于目前时间的0到42的整数。
③这里介绍一个相当优秀的的随机数发生器程序,能应用于许多领域。
<script language="JavaScript"><!--
// The Central Randomizer 1.3 (C) 1997 by Paul Houle
([email protected])
// See: http://www.msc.cornell.edu/~houle/javascript/randomizer.html
rnd.today=new Date();
rnd.seed=rnd.today.getTime();
function rnd() {
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};
function rand(number) {
return Math.ceil(rnd()*number);
};
// end central randomizer. -->
</script>
二、vbscript(asp) 随机数
vbs生成随机数
一个无指定范围的随机数
Function GetRandomize(numstr)
Randomize
GetRandomize = Int((numstr * Rnd) + 1)
End Function
生成指定范围的随机数
Function MyRnd(NumMin,NumMax)
Randomize
MyRnd = Int(Rnd * (NumMax - NumMin + 1)) + NumMin
End Function