SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript Random Number generator

    Guys,

    I have the below script that generates a random number, but struggling to modify it such that it generates a random number less than or equal to 2,147,483,647

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function dorand(){
    var dateObject = new Date();
            var uniqueId =
              dateObject.getMonth() + '' +
              dateObject.getDate() + '' +
              dateObject.getTime();
            var randomnumber = Math.floor(Math.random() * 9001);
            
            if (uniqueId.length > 10){
           
                                    uniqueId = uniqueId.substring(0, 10);
    
            }
            var uniqueId = parseInt(randomnumber) + parseInt(uniqueId);
            document.write(uniqueId);
    
            }
    
            dorand();
        </script>
       </head>
       <body>
       </html>

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function dorand(){
    var dateObject = new Date();
            var uniqueId =
              dateObject.getMonth() + '' +
              dateObject.getDate() + '' +
              dateObject.getTime();
            var randomnumber = Math.floor(Math.random() * 9001);
    
            if (uniqueId.length > 10){
    
                                    uniqueId = uniqueId.substring(0, 10);
    
            }
            var uniqueId = Math.min(parseInt(randomnumber) + parseInt(uniqueId),2147483647);
            document.write(uniqueId);
    
            }
    
            dorand();

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank You


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
  •