Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to use "Math.random()" to generate a random number in JavaScript?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 03-02-2010
Member
 
Join Date: Jul 2006
Posts: 218
How to use "Math.random()" to generate a random number in JavaScript?
  

I have started doing the coding in JavaScript programs some months ago. Now I can make a better programs in JavaScript. The problem I am facing now is while using the Math.random(). Can anyone explain me how to use "Math.random()" to generate a random number..?? I have tried a lot of different things but was not successful. The sample of coding will be appreciable. Expecting some help as soon as possible.!

__________________
~*~Silent~Kid~*~
"To The World You May Be Just One Person, But To One Person You May Be The World"
Reply With Quote
  #2  
Old 03-02-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
Re: How to use "Math.random()" to generate a random number in JavaScript?

If you want to generate a random number between 1 and 200, you can do this by using the random() method. Use the following code for better explanation :
Code:
var rand_no = Math.random();
rand_no = rand_no * 200;
alert(rand_no);
The first step is to multiply the long decimal random number generated by the random() method with 200. The code above will give us a random number between 0 and 10. All numbers will be a little more than 0 and a little less than 200.
Reply With Quote
  #3  
Old 03-02-2010
Member
 
Join Date: Oct 2005
Posts: 2,387
Re: How to use "Math.random()" to generate a random number in JavaScript?

I would like to suggest you to use the JavaScript ceil() method. By using this method, you can round a decimal number to the next higher integer. Check the following example that demonstrates the same :
Quote:
Math.ceil(3.357) -->gives 4
Math.ceil(32.98)-->gives 33
Math.ceil(1.006)-->gives 2
JavaScript generates random numbers based on a formula. Hope that you will get something useful from this.
Reply With Quote
  #4  
Old 03-02-2010
Member
 
Join Date: Nov 2008
Posts: 1,192
Re: How to use "Math.random()" to generate a random number in JavaScript?

While creating the applications in JavaScript like dice, random image script, or random link generator, a random number plays an important role. If you want to generate a random number in JavaScript, you can use the mentioned code :
Code:
var randomnumber=Math.floor(Math.random()*101)
In the above coding 101 indicates that the random number will fall between 0-100. You can change the number accordingly.
Reply With Quote
  #5  
Old 03-02-2010
Member
 
Join Date: Jul 2006
Posts: 442
Re: How to use "Math.random()" to generate a random number in JavaScript?

Whenever you need a random number, you can use the "Math.random()" every time. Also the main thing in Math.random() is that you have to set the range. Check the following example where I have used it.
Code:
now = new Date();
seed = now.getSeconds();
var random_number = Math.random(seed);
var range = random_number *10;
The above code will begin at 0 and will end with 9. This is the only reason why we are multiplying the random_number with 10. If your "random_number" equalled .5026 and multiplying by 10 gives us 5.026. The overall range of numbers to the left of the decimal point will now be the 0 - 9. You will get the option 0-99 if you multiply by 100.
__________________
"When they give you ruled paper, write the other way..." J.R.J.
Reply With Quote
  #6  
Old 03-02-2010
Member
 
Join Date: Jul 2006
Posts: 286
Re: How to use "Math.random()" to generate a random number in JavaScript?

If you want to generate the random numbers from a given range, follow the steps for that :
  1. First you will have to get the number between 0 and 10 from random() method.
  2. After that you will have to multiply that number with the difference of upper value and one less than the lower value of the range.
  3. For converting that into an integer. you will have to use floor().
  4. Lastly add the lower value of the range.
Check the following example :
Code:
var rand_no = Math.floor((20-9)*Math.random()) + 10;
alert(rand_no);
The code above generates a random number between 10 and 20.
__________________
IF you hate me, please don't mention it. I know who hates me and who doesn't. You really do not have to make fun of people....
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to use "Math.random()" to generate a random number in JavaScript?"
Thread Thread Starter Forum Replies Last Post
Generate some quality of random number via Random Number Generation function aMADeO! Windows Software 4 13-01-2012 04:07 PM
Logitech G15 (blue) random "restarting" Zia 7 Hardware Peripherals 5 10-06-2010 06:47 AM
"Access denied" for random files and folders Dakshi Windows XP Support 2 22-10-2009 02:05 AM
Generate random number using VB Bambina Software Development 2 02-09-2009 10:56 PM
Random IE7 "cannot display webpage" errors Viensterrr Windows Vista Network 2 04-09-2007 08:21 PM


All times are GMT +5.5. The time now is 10:32 AM.