Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Lightbulb Random number prototype and tab columns

    Simple prototype to return random number from string

    Bonus: Small <pre> element tag display of array in tabbed columns.

    See example usage below:

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title> Tab columns of random numbers in array </title>
    </head>
    <body>
    <pre id="debug"></pre>
    
    <script type="text/javascript">
    String.prototype.rnd = function() { var R = this*1;  return Math.floor(Math.random()*R); }
    
    Array.prototype.tabColumns = function(W) {
      str = '';  var arr = this.slice(0);
      while(arr.length > 0) { str += arr.splice(0,W).join('\t')+'\n'; }  // or .join(',\t')
      return str;
    }
    
    var arr = [];                                         // create array
    for (var i=0; i<100; i++) { arr.push('100'.rnd()); }  // fill array with random numbers 0 to 99
    arr.sort();
    document.getElementById('debug').innerHTML = arr.tabColumns(10);  // display in columns
    document.getElementById('debug').innerHTML +='Original array\n'+arr.join(',');
    </script>
    </body>
    </html>
    Last edited by jmrker; 12-14-2013 at 03:18 AM.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    question, wouldn’t it be more sensible, to put the rnd() function on the Number prototype? (why should I be able to get a random number out of a non-numeric string?)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Lightbulb

    I had that as the original thought,
    but it only works if you precede the function with a variable and not an actual number.

    I was trying to not create a new variable just to call the function,
    and in the format of 100.rnd() it caused an error.

    Here is the modified code using Number and an extra variable:
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title> Tab columns of random numbers in array </title>
    </head>
    <body>
    <pre id="debug"></pre>
    
    <script type="text/javascript">
    Number.prototype.rnd = function() { var R = this*1;  return Math.floor(Math.random()*R); }
    
    Array.prototype.tabColumns = function(W) {
      str = '';  var arr = this.slice(0);
      while(arr.length > 0) { str += arr.splice(0,W).join('\t')+'\n'; }  // or .join(',\t')
      return str;
    }
    
    var arr = [];                                         // create array
    var arrMax = 100;  
    for (var i=0; i<100; i++) { arr.push(arrMax.rnd()); }  // fill array with random numbers 0 to 99
    arr.sort();
    document.getElementById('debug').innerHTML = arr.tabColumns(10);  // display in columns
    document.getElementById('debug').innerHTML +='Original array\n'+arr.join(',');
    </script>
    </body>
    </html>
    I have since figured out a way to not use the extra variable 'arrMax'
    which also shortened the original .rnd() prototype function.

    The format to call the function would be, for example, Number(100).rnd();
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title> Tab columns of random numbers in array </title>
    </head>
    <body>
    <pre id="debug"></pre>
    
    <script type="text/javascript">
    Number.prototype.rnd = function() { return Math.floor(Math.random()*this); }
    
    Array.prototype.tabColumns = function(W) {
      str = '';  var arr = this.slice(0);
      while(arr.length > 0) { str += arr.splice(0,W).join('\t')+'\n'; }  // or .join(',\t'); } 
      return str;
    }
    
    var arr = [];                                         // create array
    for (var i=0; i<100; i++) { arr.push(Number(100).rnd()); }  // fill array with random numbers 0 to 99
    arr.sort();
    document.getElementById('debug').innerHTML = arr.tabColumns(10);  // display in columns
    document.getElementById('debug').innerHTML +='Original array\n'+arr.join(',');
    </script>
    </body>
    </html>
    Last edited by jmrker; 12-15-2013 at 03:57 PM.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Code:
    100.rnd()
    is an error

    Code:
    100 .rnd()
    is cool, as is

    Code:
    (100).rnd()
    and

    Code:
    Number('100').rnd()
    since i was bored, i shortened the functions:
    Code:
    Array.prototype.tabColumns = function(n) {
      return this.slice(-n).map(function(a,i){
        return this.slice( i= (i * n), i+n).join("\t");
      }, this).join("\n");
    }
    
    
    Number.prototype.rnd = function(){ return Math.floor(Math.random()*this); }
    Last edited by rnd me; 12-14-2013 at 05:45 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Thumbs up

    Thanks.

    I had not realized the (100).rnd() or 100 .rnd() formats as a possibility.

    And the .tabColums() function was for special purpose displays
    but serves as a good example of incorporating the .map() function

    Finally, your .rnd() is the same as my final version of post #2
    but is good to highlight it as it had been changed from first attempt.

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Lightbulb

    OK, just playing now, but I added another prototype function
    to create a <table> within a <div> element similar to the tab display in the <pre> tag.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title> Tab columns of random numbers in array </title>
    </head>
    <body>
    <pre id="debug"></pre>
    <div id="debug2"></div>
    
    <script type="text/javascript">
    
    Number.prototype.rnd = function() { return Math.floor(Math.random()*this); }
    
    Array.prototype.tabColumns = function(n) {
      return this.slice(-n).map(function(a,i){
        return this.slice( i= (i * n), i+n).join("\t");
      }, this).join("\n");
    }
    
    Array.prototype.tableDisplay = function(n) {
      return this.slice(-n).map(function(a,i){
        var row = this.slice( i= (i * n), i+n).join("</td><td>");
        return '<tr><td>'+row+'</td></tr>';
      }, this).join("\n");
    }
    
    
    // Example usage:
    var arr = [];                                         // create array
    for (var i=0; i<100; i++) { arr.push((100).rnd()); }  // fill array with random numbers 0 to 99
    arr.sort(function(a,b){return a-b});
    document.getElementById('debug').innerHTML = arr.tabColumns(10);  // display in columns
    document.getElementById('debug').innerHTML +='<p>Original array\n'+arr.join(',');
    
    document.getElementById('debug2').innerHTML
      = '<table border="1" width="25%"><tbody>'+arr.tableDisplay(10)+'</tbody></table>';
    </script>
    </body>
    </html>
    NOTE: Also added a modification to the sort() for the numbers display.
    Use .sort() if alpha is the primary display determinant.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •