我的问题:
如何在Javascript中创建具有给定均值和标准差(sd)的随机数列表?
例:
我想创建一个包含5个随机数的列表,范围在1到10之间.结果均值应为5,标准差应为2.
到目前为止我做了什么:
我的想法是(http://jsfiddle.net/QASDG/3/):
注意:我还没有添加SD,因为代码不是很优雅.
我发现该站点的另一个脚本声明:"此代码的目标是生成一个集合,其中包含5个正态分布的随机数,平均值为1.0,标准差为0.5." http://rosettacode.org/wiki/Random_numbers#JavaScript.我试图通过添加"Math.floor"来调整它,并改变了"for"-loop(i <5)中的条件.
function randomNormal() {
return Math.floor(Math.cos(2 * Math.PI * Math.random()) * Math.sqrt(-2 * Math.log(Math.random())))
}
var a = []
for (var i=0; i < 5; i++){
a[i] = randomNormal() / 2 + 1
}
Run Code Online (Sandbox Code Playgroud)
我想如何修改我的代码:
我不太确定我是否理解了这段代码的数学部分.根据网页,它将创建一个正常的分布(这是伟大的,但没有必要).所以我需要的是:
非常感谢!
场景:
如果用户将鼠标悬停在文本(例如 h1 标签)上,它应该更改为新文本。新文本应顺利显示。
到目前为止我所做的:
我能够用带有“display:none”和“content:'This is the new text'”属性的新文本替换文本。我的问题是新文本显示不流畅(它不会淡入/过渡)。我也尝试使用不透明度,但它不会替换我的旧文本(相反,它只是消失了,新文本出现在它旁边)。
这是一个JSFiddle和代码片段:
.my_div {
background-color: red;
}
.my_div:hover {
background-color: green;
transition: all 500ms ease-in-out;
}
.my_div:hover .title span {
display: none;
}
.my_div:hover .title:after {
content: "A wild text appears";
}Run Code Online (Sandbox Code Playgroud)
<div class="my_div">
<h1 class="title"><span>This is the old text</span></h1>
</div>Run Code Online (Sandbox Code Playgroud)
我想做的事:
我希望在设定的持续时间(例如3秒)后隐藏元素或图像.不幸的是,如果我使用.hide(3000)方法,它会产生这种"缩小"的效果.如果我使用.fadeOut(3000)方法,它当然会逐渐淡出.我只想显示元素3秒(没有任何效果或平滑过渡),在这3秒后它应该立即消失.
HTML:
<button class="buttonHide">Hide</button>
<img id="loadinggif" src="http://hosting-nation.ca/templates/hn1/images/loading.gif"/>
Run Code Online (Sandbox Code Playgroud)
JQuery的:
$(document).ready(function(){
$(".buttonHide").click(function(){
$("#loadinggif").hide(3000); //or fadeOut(3000)? Both doesn't have the desired effect.
});
});
Run Code Online (Sandbox Code Playgroud)
非常感谢!
我想创建一个"冒泡排序"方法,这意味着我在一个数组中取两个连续元素,比较它们,如果左边元素大于右边元素,它们应该切换位置.我想重复它,直到我的数组按升序排序.
我的代码只能部分工作.如果我的数组太大,就不会发生任何事情(我必须用CTRL + C退出ruby).对于小于5个元素的数组,我的代码工作正常:
def bubbles(array)
while array.each_cons(2).any? { |a, b| (a <=> b) >= 0 }
# "true" if there are two consecutives elements where the first one
# is greater than the second one. I know the error must be here somehow.
array.each_with_index.map do | number, index |
if array[index + 1].nil?
number
break
elsif number > array[index + 1]
array[index], array[index + 1] = array[index + 1], array[index] # Swap position!
else
number
end
end
end
p …Run Code Online (Sandbox Code Playgroud)