Results 1 to 2 of 2

Thread: Need help with Math.floor and Math.random

  1. #1
    Join Date
    Aug 2012
    New York

    Cool Need help with Math.floor and Math.random

    Please don't judge! I suck!

    Okay now that that's out of the way here is what I'm trying to do:

    I downloaded source code for a lorem ipsum generator and I can't figure out how to change to the amount of commas, and length of sentences in the supplied JS file.

    This it the current .JS

    Originally the var line was: var sentence_number = Math.floor( (Math.random()+1) * 1 );
    Was: var sentence_number = Math.floor( (Math.random()+4) * 2 );

    But that was creating abnormally long paragraphs.

    //document ready is a safety precaution that makes sure all of the HTML document has loaded before we try to add behavior.
    //Start event listener for click
    $("#ipsum-form").submit(function() { 
      var paragraphs = '';
    //Determine which of the check boxes is checked 
      var chosen_button = $("#ipsum-form input[name='choice']:checked").val();
    //Grab the paragraph number the user enters
      var paragraph_number = $("#paragraph_count").val();
    //Define var words as an empty array
      var words = [];
    //Create an array of words to randomize later
      var words_bob = ["jargon ipsum", "storytelling", "adlob", "brand voice", "above the line", ":30", "blueskying", "tissue session", "case study", "case study", "psychographics", "direct response", "pain points", "doesnít telegraph", "use case", "fearless conversation", "deep dive", "emotional territory", "surprise and delight", "mood board", "wild posting", "greek the copy", "out of home", "ladder up", "dial back", "never been done before", "brand platform", "tipping point", "closing the loop", "leverage"];
      var words_nobob = ["jargon ipsum", "itís a conversion play", "digital activation", "social listening", "extra bandwidth", "direct response", "itís a PR play", "earned media", "consumer facing", "reverse auto targeted", "hero image", "fearless conversation", "lower funnel", "3 million impressions", "geotargetting", "banner burnout", "hotlinking", "rich media", "skyscraper", "interstitial", "experienced by few seen by many", "market research insight", "experiential", "click through rate", "CTA", "micro targeted"]; 
      var words_all = words_bob.concat(words_nobob);
    //ELSE IF determines which array of words to show the user
      if (chosen_button == "straight-up") {
       words = words_all;
    } else if (chosen_button == "all-bob") {
       words = words_bob; 
    } else {
    words = words_nobob; }
    //Vary the number of sentences in each paragraph randomly
    var sentence_number = Math.floor( (Math.random()+1) * 1 );
    //Use a function that randomizes the contents of an array
      function fisherYates(words) {
        var i = words.length, j, tempi, tempj;
        if ( i == 0 ) return false;
        while ( --i ) {
           j = Math.floor( Math.random() * ( i + 1 ) );
           tempi = words[i];
           tempj = words[j];
           words[i] = tempj;
           words[j] = tempi;
           return words;
    //Start the first FOR loop that builds sentences from words
    for ( var z = 0; z < paragraph_number; z++ ) {
      var sentence_group = '';
    //Start the second FOR loop that builds sentence groups from sentences
    for ( var y = 0; y < sentence_number; y++ ) {
    //Start the third FOR loop that builds paragraphs from sentence groups
    for ( var x = 0; x < words.length; x++ ) {
    //Create a variable for the randomized array of words
      var words_random = fisherYates(words);
    //Convert array to string with no commas or quotes, add period to end
      var sentence = words_random.toString().replace(/,/g, ' ') + '. ';
    //Capitalize first letter in string
      function capitalizeFirstLetter(string) {
          return string.charAt(0).toUpperCase() + string.slice(1);
      var sentence_capped = capitalizeFirstLetter(sentence);
    //End the first FOR loop that builds sentences from words
      sentence_group += sentence_capped;  
    //End the second FOR loop that builds sentence groups from sentences
      paragraphs+='<p>' + sentence_group + '</p>';
    //End the third FOR loop that builds and spaces paragraphs from sentence groups
    //Prevent form from actually submitting so page does not reload
    return false; 
    //End jQuery event listener
    //End document ready

  2. #2
    Join Date
    Dec 2002
    Seattle, WA
    Math.random() generates a random number between 0 and 1.
    Math.floor((Math.random() + 1) * 1);
    would amount to 0...1 + 1 * 1 = 1 or 2
    Math.floor((Math.random() + 4) * 2);
    4-5 * 2 = 8 or 10

    if you want to get Math.random() to generate numbers within a set range you could do something like
    function rand(min, max)
        return Math.floor(Math.random() * (max - min + 1)) + min;
    Last edited by ShrineDesigns; 04-29-2014 at 04:44 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Recent Articles