|
| |||||||||
| Tags: ceil method, javascript, math random, programs, random link generator |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| ||||
| ||||
| 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
| ||||
| ||||
| 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); |
|
#3
| ||||
| ||||
| 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:
__________________ Grand Theft Auto 4 PC Video Game |
|
#4
| |||
| |||
| 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) |
|
#5
| ||||
| ||||
| 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;
__________________ "When they give you ruled paper, write the other way..." J.R.J. |
|
#6
| ||||
| ||||
| 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 :
Code: var rand_no = Math.floor((20-9)*Math.random()) + 10; alert(rand_no);
__________________ 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 |
| |
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 | 4 Weeks Ago 05:07 PM |
| "Access denied" for random files and folders | Andariel Halo | Windows XP Support | 2 | 22-10-2009 03:05 AM |
| Generate random number using VB | Bambina | Software Development | 2 | 02-09-2009 11:56 PM |
| Random "rebooted from a bugcheck" reboots | alexander750 | Window 2000 Help | 1 | 06-09-2007 11:56 AM |
| Random IE7 "cannot display webpage" errors | Sarah_Long | Windows Vista Network | 2 | 04-09-2007 09:21 PM |