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

#1
 Silent~Kid 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"
#2
 absolute55 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;
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.
#3
 Reegan Member Join Date: Oct 2005 Posts: 2,389
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.
#4
 void 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.
#5
 kyosang 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.
#6
 Deabelos 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;
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....

 Thread Tools Search this Thread Show Printable Version Email this Page 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 05: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 08:28 PM.