随机数脚本 [英] Random Number Script

查看:81
本文介绍了随机数脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个脚本会将一个随机数写入文档写入标签,

我一直试图让它写入
$ b之外的输入表格框$ b javascript,任何帮助表示赞赏。谢谢Joe

http://web2jo.com/Work/ Random_Temp.html

Hi, This script will write a random number into a document write tag,
I''ve been trying to get it to write into a input form box outside the
javascript, any help is appreciated. Thanks Joe

http://web2jo.com/Work/Random_Temp.html

推荐答案

" Papajo" <做***** @ webtv.net>在消息中写道

news:21 *************** @ storefull-3336.bay.webtv.net ...
"Papajo" <do*****@webtv.net> wrote in message
news:21***************@storefull-3336.bay.webtv.net...
这个脚本会将一个随机数写入文档写入标记,
我一直试图将它写入
javascript之外的输入表单框,任何帮助表示赞赏。谢谢Joe

http://web2jo.com/Work/ Random_Temp.html
Hi, This script will write a random number into a document write tag,
I''ve been trying to get it to write into a input form box outside the
javascript, any help is appreciated. Thanks Joe

http://web2jo.com/Work/Random_Temp.html




这会有帮助吗?


< html>

< head>

< title> Random.htm< / title>

< script type =" text / javascript">

函数getRandomNum(lbound,ubound){

var num = Math.floor(Math.random()*(ubound - lbound))+ lbound;

文件.forms [0] .text.value = num;

}

< / script>

< / script>

< / head>

< body>

< form>

< input type =" text" ;命名= QUOT;文本"大小= QUOT; 6英寸readonly>

< input type =" button" value =" Random"

onclick =" getRandomNum(111111,999999)">

< / form>

< ; / body>

< / html>



Will this help?

<html>
<head>
<title>Random.htm</title>
<script type="text/javascript">
function getRandomNum(lbound, ubound) {
var num = Math.floor(Math.random() * (ubound - lbound)) + lbound;
document.forms[0].text.value = num;
}
</script>
</script>
</head>
<body>
<form>
<input type="text" name="text" size="6" readonly>
<input type="button" value="Random"
onclick="getRandomNum(111111,999999)">
</form>
</body>
</html>


我不得不稍微修改脚本,但效果很好,谢谢Joe

< script type =" text / javascript">

函数getRandomNum(lbound,ubound){

var num =(Math.floor( Math.random()*(ubound - lbound))+ lbound);

document.forms [0] .text.value = num;

}

< / script>

< / head>

< body>

< form>

< input type =" text"命名= QUOT;文本"大小= QUOT; 6英寸readonly>


< input type =" button" value =" Random"

onclick =" getRandomNum(111111,999999)">

< / form>

I had to modify the script a little but it works great, Thanks Joe
<script type="text/javascript">
function getRandomNum(lbound, ubound) {
var num = (Math.floor(Math.random() * (ubound - lbound)) + lbound);
document.forms[0].text.value = num;
}
</script>
</head>
<body>
<form>
<input type="text" name="text" size="6" readonly>

<input type="button" value="Random"
onclick="getRandomNum(111111,999999)">
</form>


McKirahan写道:
McKirahan wrote:
" Papajo" <做***** @ webtv.net>写了[...]
"Papajo" <do*****@webtv.net> wrote [...]
这个脚本会将一个随机数写入文档写入标签,
我一直试图让它写入外面的输入表单框
javascript,任何帮助表示赞赏。谢谢Joe

http://web2jo.com/Work/ Random_Temp.html
这会有帮助吗?
Hi, This script will write a random number into a document write tag,
I''ve been trying to get it to write into a input form box outside the
javascript, any help is appreciated. Thanks Joe

http://web2jo.com/Work/Random_Temp.html
Will this help?




不是真的。

[...]
function getRandomNum(lbound,ubound){
var num = Math.floor(Math.random()*(ubound - lbound))+ lbound;
document.forms [0] .text.value = num;
}



Not really.
[...]
function getRandomNum(lbound, ubound) {
var num = Math.floor(Math.random() * (ubound - lbound)) + lbound;
document.forms[0].text.value = num;
}




- 让lbound为1.

- 让ubound为10.

- 让Math.random()返回0.99。

- 返回


Math.floor(0.99 *(10 - 1))+ 1

= Math.floor(8.91)+ 1

= 8 + 1

= 9


自Math.random()总是返回小于1的东西,不可能

结果是10.但请记住:我们说的是一个随机整数

_closed_interval [x,y],即包含的边界。如果

上限为y且y为整数,则返回y的概率应为

大于零。这意味着你的算法在右侧开放区间[x,y)中计算一个随机的

整数,这不是指定的




这个问题可以通过使用Math.round()代替Math.floor()和

舍入计算的浮点数来解决。使用


函数getRandomNum(lbound,ubound)

{

返回Math.round(Math.random()*(ubound) - lbound)+ lbound);

}


document.forms [0] .text.value = getRandomNum(1,10);


- 让lbound为1.

- 让ubound为10.

- 让Math.random()返回0.99。

- 返回


Math.round(0.99 *(10 - 1)+ 1)

= Math.round(0.99 * 9 + 1)

= Math.round(8.91 + 1)

= Math.round(9.91)

= 10


- 让lbound为1.

- 让ubound为10.

- 让Math.random()返回0.01。

- 返回


Math.round(0.01 *(10 - 1)+ 1)

= Math.round(0.01 * 9 + 1)

= Math.round(0.09 + 1)

= Math.round(1.09)

= 1


因为Math.random()永远不会返回小于0的东西,并且总是

小于1的东西(ECMAScript 3,15.8.2.14),那个alg orithm是安全的。

PointedEars



- Let lbound be 1.
- Let ubound be 10.
- Let Math.random() return 0.99.
- That returns

Math.floor(0.99 * (10 - 1)) + 1
= Math.floor(8.91) + 1
= 8 + 1
= 9

Since Math.random() always returns something less than 1, it is not possible
that the result is 10. But remember: we are speaking of a random integer
in the _closed_ interval [x, y], that is, the bounds are included. If the
upper bound is y and y is an integer, the probability of returning y should
be greater than zero. Meaning that your algorithm computes a random
integer in the right-hand side open interval [x, y) which is not what was
specified.

This problem can be solved by using Math.round() instead of Math.floor() and
rounding a computed floating-point number. With

function getRandomNum(lbound, ubound)
{
return Math.round(Math.random() * (ubound - lbound) + lbound);
}

document.forms[0].text.value = getRandomNum(1, 10);

- Let lbound be 1.
- Let ubound be 10.
- Let Math.random() return 0.99.
- That returns

Math.round(0.99 * (10 - 1) + 1)
= Math.round(0.99 * 9 + 1)
= Math.round(8.91 + 1)
= Math.round(9.91)
= 10

- Let lbound be 1.
- Let ubound be 10.
- Let Math.random() return 0.01.
- That returns

Math.round(0.01 * (10 - 1) + 1)
= Math.round(0.01 * 9 + 1)
= Math.round(0.09 + 1)
= Math.round(1.09)
= 1

Since Math.random() will never return something less than 0, and always
something less than 1 (ECMAScript 3, 15.8.2.14), that algorithm is safe.
PointedEars


这篇关于随机数脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆