开发者

Javascript random number picker not working

I'm trying to make a simple javascript application to pick a random number between 1 and a number I specify in an input field, so I can pick a random winner for a Christmas competition.

Below is the code I开发者_StackOverflow've got so far. I don't think it can be far off, can you help me as it's not updating the input field to display the random number:

<script type="text/javascript" language="javascript">
  function randomWinner() {
    var topNumber = topNumber.value;    
    var randomnumber=Math.floor(Math.random() + topNumber);
    winningNumber.value=randomnumber;
    return true;
  }
</script>

<form name="selectWinner">
Pick random number between 1 and <input name="topNumber" value="100"><br /><br />
The winning number: <input name="winningNumber" readonly="true"><br /><br />
<input type="button" value="Pick Winner" OnClick="randomWinner();">
</form>


You need to multiply your topNumber with the random number between 0 and 1:

var randomnumber=Math.floor(Math.random() * topNumber);

And to get a random integer of a specific range:

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}


You are adding a string value to the random number, which gives you for example 0.98764912837465 + "100" which gives you the string "0.98764912837465100". The floor function manages to turn it back into a string, but it's still never large enough to become anything other than zero.

You need to multiply instead. That gives you a number in the range 0 to topnumber-1, so you have to add one to get it in the range 1 to topnumber:

var randomnumber=Math.floor(Math.random() * topNumber) + 1;


You can't access the input field just by using its name. Try adding an ID and using getElementById:

<input name="topNumber" id="topNumber" value="100">
<input name="winningNumber" id="winningNumber" readonly="true">

JavaScript:

function randomWinner() {
    var topNumberInput = document.getElementById('topNumber');
    var topNumber = parseInt(topNumberInput.value, 10);
    var randomnumber= 1 + Math.floor(Math.random() * topNumber);
    var winningNumber = document.getElementById('winningNumber');
    winningNumber.value=randomnumber;
    return true;
}

In addition, I've used parseInt to make sure were working with a proper number.

Working example: http://jsbin.com/uwude4


You have to multiply the topNumber but you have to increase the topNumber + 1, to include the range, so topNumber = 101 results in a maximum randomNumber of 100.

var randomNumber = Math.floor(Math.random() * topNumber)


Math.random() generates a number between 0 and 1. Here is what you need:

var randomnumber=Math.floor(Math.random()*topNumber)+1;

Math.random() will generate a value in range [0..1]. [0..1] multiplied on topNumber will result in [0..topNumber] range. However, the Math.floor() will limit it to [0..(topNumber-1)]. Now we add 1 and get [1..topNumber].


            var randomnumber=Math.floor(Math.random()*n)

This will give u a random no between 0 to n..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜