Results 1 to 6 of 6

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

  1. #1
    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. #2
    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.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    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 :
    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. #4
    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. #5
    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. #6
    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....

Similar Threads

  1. Replies: 4
    Last Post: 13-01-2012, 05:07 PM
  2. Logitech G15 (blue) random "restarting"
    By Zia 7 in forum Hardware Peripherals
    Replies: 5
    Last Post: 10-06-2010, 06:47 AM
  3. "Access denied" for random files and folders
    By Dakshi in forum Windows XP Support
    Replies: 2
    Last Post: 22-10-2009, 02:05 AM
  4. Generate random number using VB
    By Bambina in forum Software Development
    Replies: 2
    Last Post: 02-09-2009, 10:56 PM
  5. Random IE7 "cannot display webpage" errors
    By Viensterrr in forum Windows Vista Network
    Replies: 2
    Last Post: 04-09-2007, 08:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,257,026.35560 seconds with 17 queries