Math.random()是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值,是Java语言经常使用代码。例如:double a=Math.random()*(3-1)+1,设置一个随机1到3的变量。java
1
2
|
Math.random()*(n-m)+m;
改正公式:Math.random()*(n+
1
-m)+m
|
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类型后接到result后面
result=result+(
char
)intValue;
}
//输出字符串
System.out.println(result);
}
|
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!"
);
}
}
|
1
|
document.write(Math.random());
|
1
|
document.write(Math.random()*(
20
-
10
+
1
)+
10
);
|
1
|
document.write(Math.random()*(n+
1
-m)+m);
|