Jump to content

Math.random


Fire Dragon

Recommended Posts

Hi!I create this kind code,but I want know one thing before I continue developing it.I want that code decreases one value,depend on what random value comes.

function decrease(){var value= Math.random()if (value<0.5){value2=value2 -10;}else{value2=value2 -20;}}

I have two questions.First,can you see any errors?Second question is about Math.random code.Like you see,this is function.If I create example button,and Math.random creates example value 0.42536.Than,I press again button.Question is,do math.random create new value?Example 0.84763.Thanks for help.I write this code to paper,and I can't test it yet,because it is part of huge code.I can't see any mistakes in it,but you can't never be sure... :) Thanks.

Link to comment
Share on other sites

Math.random() always creates a new number, you can check this by putting it's result in an alert message. Also, if you put it within a Math.ceil() you get whole numbers ie 1 & 6 not 0.666 & 5.32656

<head><script>var value2=0;function decrease(){  var value=Math.ceil(Math.random()*10);  alert("Random number is "+value);    if (value<5)    {      value2=value2-10;      document.getElementById('val2').value=value2;    }    else    {      value2=value2-20;      document.getElementById('val2').value=value2;    }}</script></head><body><input type="button" value="button" onclick="decrease()" /><p>Value 2 <input type="input" value="0" id="val2" /></p></body>

Link to comment
Share on other sites

Math.random() always creates a new number, you can check this by putting it's result in an alert message.  Also, if you put it within a Math.ceil() you get whole numbers ie 1 & 6 not 0.666 & 5.32656
<script>var value2=0;function decrease(){  var value=Math.ceil(Math.random()*10);  alert("Random number is "+value);    if (value<5)    {      value2=value2-10;      document.getElementById('val2').value=value2;    }    else    {      value2=value2-20;      document.getElementById('val2').value=value2;    }}</script><body><input type="button" value="button" onclick="decrease()" /><p>Value 2 <input type="input" value="0" id="val2" /></p></body>

I can't get it work.I tried modify and only copy & paste,but when I press button,I can't see any alerts,or field's value won't change.What I did wrong?
Link to comment
Share on other sites

I can't get it work.I tried modify and only copy & paste,but when I press button,I can't see any alerts,or field's value won't change.What I did wrong?

What exactly did you "modify"? I don't know what's wrong unless you show me what changes you have made. :)
Link to comment
Share on other sites

It's ok, these things happen!!  :)

Yeah,you are right :) By the way,I tried set that if value is 0 or less,code directes you to new URL,but I can't get it work right now.I tried if else statement,but I don't know why it wont work.Here is my code:
   else if (value<=0)   {     window.location("http://www.google.com")   }

I put it between if and else statements,but nothing happens when value is equal or under 0.What can be wrong?I also tried create own function,what checks value when user presses button,but it won't work.

Link to comment
Share on other sites

By the way,I tried set that if value is 0 or less,code directes you to new URL,but I can't get it work right now.

That won't work because if you look at this line of code var value=Math.ceil(Math.random()*10); i used Math.ceil which rounds a number up to the nearest integer, it will never be 0.change it to Math.floor like so:
var value=Math.floor(Math.random()*10);

your if statement will now work for a 0. :)

Link to comment
Share on other sites

Like usual,I can't get it work right now :) Here is my code:

<script>var value2=100;function decrease(){ var value=Math.floor(Math.random()*10); alert("Random number is "+value);   if (value<5)   {     value2=value2-10;     document.getElementById('val2').value=value2;   }     else if (value2<=0)  {    window.location("http://www.google.com")    document.getElementById('val2').value=value2;  }   else   {     value2=value2-20;     document.getElementById('val2').value=value2;   }}</script><body><input type="button" value="button" onclick="decrease()" /><p>Value 2 <input type="input" value="100" id="val2" /></p></body>

What is wrong now?I tried,but it doesn't activate window.location,even valu2 is 0 or less.

Link to comment
Share on other sites

window.location("http://www.google.com") :) window.location="http://www.google.com" :D if (value<5) :) else if (value2<=0) :( if (value<5&&value>1) :) else if (value2==0) :blink:

<script>var value2=100;function decrease(){var value=Math.floor(Math.random()*10);alert("Random number is "+value);  if (value<5&&value>1)  {    value2=value2-10;    document.getElementById('val2').value=value2;  }    else if (value2==0) {   window.location="http://www.google.com";   document.getElementById('val2').value=value2; }  else  {    value2=value2-20;    document.getElementById('val2').value=value2;  }}</script><body><input type="button" value="button" onclick="decrease()" /><p>Value 2 <input type="input" value="100" id="val2" /></p></body>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...