TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   How to use "Math.random()" to generate a random number in JavaScript? (http://forums.techarena.in/software-development/1298507.htm)

 Silent~Kid 03-02-2010 04:28 AM

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.!

 absolute55 03-02-2010 04:36 AM

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.

 Reegan 03-02-2010 04:41 AM

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.

 void 03-02-2010 04:46 AM

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.

 kyosang 03-02-2010 04:55 AM

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.

 Deabelos 03-02-2010 05:06 AM

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.

 All times are GMT +5.5. The time now is 01:09 PM.