SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Math.random

  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Math.random

    I know about the Math.random function and I was doing a lil' bit of studying at the W3 schools website but I only saw the highest random number that could be produced is 1-100, can the random function go any higher than that? Like say for instance going to 1000?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,044
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Hi Hikaru Kagi, welcome to the forums,

    I think you were looking only at the example, and misread the info.

    Math.random() returns a float between 0 and 1

    It has quite a few decimal places, so you could multiply times 1000 or even 10000000000000000 if you wanted to. (don't forget to "floor" it)

    I'm not sure how "random" it is though. Best to have maybe another one or a Date method in the mix too.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah ok, is it possible to specify within in it? Well to be more specific like only be random up to 200, skip a couple of hundreds and then start again at maybe 600 then random it out all the way to 1000?

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,044
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    I'm not sure I understand what you're asking. You want a random Int between 0-200,600-1000 {i.e. 0-1000 but excluding 201-599?)

    If so, you culd write a function that tests for the value and doesn't "return" until it meets the criteria.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much! I just wasn't sure whether or not it could be done, but I guess I shouldn't doubt the power of JavaScript eh? but anyway, thank you for your time to help out a newbie such as myself.

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,044
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    No problem. BTW when I said "floor" before, I should have added "round" and "ceil". The main thing is to get the float to an int without decimals somehow.

    Yes, javascript can do some amazing things. Once you get familiar with the data types, syntax and an overview of the methods, a good way to learn is to think of something (simple at first) you would like to do, then do it!

    Being careful inside loops, generous use of
    alert('I'm here');
    alert('someVar is ' + typeof someVar);
    alert('someVar = ' + someVar);
    can help you to see what's going on.

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You should be able to get integers between 0 and 9007199254740992 out of Math.random.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why that huge number? Why can't it be just 1000000000000000? Or something simple like that?


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
  •