math.random()
来源:互联网 发布:javascript 页面刷新 编辑:程序博客网 时间:2024/06/05 05:07
math.random()编辑
本词条缺少信息栏,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧!
Math.random()是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值,是Java语言常用代码。
例如:var a:Number=Math.random()*2+1,设置一个随机1到3的变量。
目录
1flash的代码编辑
Math.random()是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值,是Java语言常用代码。
例如:var a:Number=Math.random()*2+1,设置一个随机1到3的变量。
2java的代码编辑
Math.random():产生一个[0,1)之间的随机数。
返回指定范围的随机数(m-n之间)的公式[1] :
1
Math.random()*(n-m+
1
)+m;
例:
生成一个6位的随机字符串:
1
2
3
4
5
6
7
8
9
10
11
public
static
void
main(String[]args){
String result=
""
;
for
(inti=
0
;i<
6
;i++){
//生成97-122的int型的整型
int
intValue=(
int
)(Math.random()*
26
+
97
);
//将intValue强制转化成char类型后接到resul后面
result=result+(
char
)intValue;
}
//输出字符串
System.out.println(result);
}
3Math.random();使用方法编辑
例:用Math类的random()方法产生一个字符,若该字符是一个大写英文字母,则输入 "Yes!",否则输出“NO”。
random()方法产生的随机数在0.0和1.0之间,乘以128后,其值在0.0和128.0之间,将它转换为char类型后,用if来判断是否在'A' 和'Z'之间。程序如下:
1
2
3
4
5
6
7
8
9
10
classIsUpper{
publicstaticvoidmain(String[]args){
charch;
ch=(
char
)(Math.random()*
128
);
if
(ch>=
'A'
&&ch<=
'Z'
)
System.out.println(
"Yes!"
);
else
System.out.println(
"No!"
);
}
}
JavaScript Math.random() 函数
Math.random() -- 返回0和1之间的伪随机数 可能为0,但总是小于1,[0,1)
1
document.write(Math.random());
返回随机数
1
document.write(Math.random()*(
20
-
10
)+
10
);
返回10-20的随机数
1
document.write(Math.random()*(n-m)+m);
返回指定范围的随机数(m-n之间)的公式[1]
- 参考资料
0 0
- Math.random()
- Math.random()
- Math.random()
- math.random()
- math.random()
- Math.random()
- math.random()
- math.random()
- math.random()
- Math.random
- math.random()
- Math.random()
- Math.random()
- Math.random()
- Math.random()
- Math类 random()与Random()
- as3:Math.random()
- 理解Math.random()随机数
- PostgreSQL change unlogged table to logged table
- 关于Android在线支付Alipay(支付宝)开发的经验分享
- 复杂链表的复制
- LINUX select()函数的应用
- Libnids中tcp流处理"tcp.c"
- math.random()
- xcode更改SVN地址
- HttpClient 教程 (二)
- jtable——数据列的type
- ffmpeg + ios 移植ffmpeg 到ios 平台上
- Java之synchronized加鎖範圍
- HttpClient 教程 (三)
- Java 线程问题
- 理解事务——原子性、一致性、隔离性和持久性