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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random phrase/line generator

    Trying to develop a small app for my site. What is the code to generate a random # of lines (1 min - 5 max) and a random phrase for each line sourced from a multidimensional array? These also need to be unduplicated (so I won't have quote 1 be on lines 1 and 4). How do I prevent random duplication with the generated lines of text? Thanks!

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,073
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question More information needed ...

    Need quite a bit more information to be of any help.

    1. What does the multi-dimension array look like?
    2. Random duplication will depend upon how many phrases and lines you have to begin with.
    3. Do you have any sample code you have attempted yet?

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    This will select (and if required sort) random numbers without duplication. The MaxValue will be the number of items in your array (here 100), and NumUnique is the random number of numbers to be selected.

    Not sure how a multi-dimensional array comes into it. Do you simply mean an array containing a large number of phrases? Is this supposed to be a sort of "Quote of the day" script?

    Code:
    <script type = "text/javascript">
    
    function getRanNums() {
    var MaxValue = 99; // (i.e. range 0 to 99 inclusive)
    var NumUnique = Math.floor(Math.random() * 5)+1;  // random number of unique random numbers 1-5
    var numArray = new Array(MaxValue+1);
    var uniqueList = new Array(NumUnique);
    for (var i = 0; i <= MaxValue; i++) {
    numArray[i] = i;
    }
    numArray.sort(randOrd);
    for (var i = 0; i< NumUnique; i++) {
    uniqueList[i] = numArray[i];
    }
    
    function randOrd(){return (Math.round(Math.random())-0.5); }
    
    function sortNumber(a,b) {
    return a - b;
    }
    
    alert ("The unsorted random numbers are:- \n" + uniqueList)
    alert ("The sorted unique random numbers are:- \n" + uniqueList.sort(sortNumber));
    }
    
    getRanNums();
    
    </script>

    “There are two kinds of failures: those who thought and never did, and those who did and never thought.” - Dr. Laurence J. Peter quotes (American "hierarchiologist", Educator and Writer, 1919-1990)
    Last edited by Philip M; 09-30-2009 at 08:34 AM.

  • #4
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Thanks. The multidimensional array is a set of 3 arrays (phrase lists). I guess it could just be 3 separate arrays. This is kind of like a 'quote of the day' but a little more complex.

    The idea is to generate a random # of lines (which your code does). For each line, it would then randomly select one phrase from a randomly selected array, without duplicating the array or the phrase.

    var array1 = new Array(5);
    array1[0] = "Duck 1";
    array1[1] = "Duck 2";
    array1[2] = "Duck 3";
    array1[3] = "Duck 4";
    array1[4] = "Duck 5";

    var array2 = new Array(5);
    array2[0] = "Goose 1";
    array2[1] = "Goose 2";
    array2[2] = "Goose 3";
    array2[3] = "Goose 4";
    array2[4] = "Goose 5";

    and so on. 3 different arrays.

    Any help is appreciated! Thanks

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Well, all you have to do is (using the above script) to select one or more unique random phrases from Array1, then another set from Array2 and then yet a third set from Array3.

    If you are saying that you want the resultant list (array) of selected phrases to be shuffled into a random order:-


    Code:
    <script type = "text/javascript">
    
    Array.prototype.shuffle = function (){  
    for(var rnd, tmp, i=this.length; i; rnd=parseInt(Math.random()*i), tmp=this[--i], this[i]=this[rnd], this[rnd]=tmp);
    }
    
    var myArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
    myArray.shuffle();
    alert (myArray);  // EXAMPLE: 6,8,2,14,11,3,15,13,7,5,4,9,10,1,12 
    </script>

    If there are three arrays but you want to select as many as 5 phrases from them then I do not see how duplication of the array (not the phrase) can be avoided.

    I am beginning to suspect that this is far more complicated than is actually needed. If the end product is to display between 1 and 5 unique phrases randomly selected from 3 arrays, then the simplest is to get rid of the three arrays and join them together into one, with the order perhaps randomised at the outset. I agree that this could generate Duck1, Duck2, Duck3, Duck4, Duck5 - but the chances are billions to one against. And does it matter?
    Last edited by Philip M; 10-01-2009 at 09:02 AM.

  • #6
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are right. I have overcomplicated what this is supposed to do. And furtehrmore, like you pointed out, it is imossible to have 5 lines and not repeat an array - if there are only 3 arrays!

    I will try to work it using the first code put against the three separate arrays. So first the code will generate the # of lines. Then it will select a random phrase from Array1 for Line1, from Array2 for Line2...

    Will post the resultant code when ready!

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by kenneth009 View Post
    I will try to work it using the first code put against the three separate arrays. So first the code will generate the # of lines. Then it will select a random phrase from Array1 for Line1, from Array2 for Line2...
    So what happens if the number of lines = 1 or 2? It seems to me that phrases from arrays 2 and 3 will be less frequently selected than those from array 1. Only one phrase at the most can be selected from array 3, even if the total number is 5. I think you ought to think this through and define what you plan to achieve more exactly/carefully. As I say, why does the selection mtter?
    Last edited by Philip M; 10-01-2009 at 03:24 PM.

  • #8
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The selection matters bec some of the phrases are number-based.
    If it was totally open the final lines might read:

    'Hello, how are you today'
    'I need $5'
    I need $10'

    It wouldn't make sense. So I want to group all the number-based phrases into one array.

    Does that make sense?

  • #9
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    my random bank note generator

    Ok not the most fancy javascript. And I would be open to anyone helping me get at an even simpler way of doing the following...

    <script language="JavaScript">

    function getaNote()
    {
    firstList= new Array ();

    firstList[0]="Give me the money";
    firstList[1]="Stay calm";
    firstList[2]="I have a bomb";
    firstList[3]="I have a gun";
    firstList[4]="I'll shoot you";
    firstList[5]="Give me all your money";
    firstList[6]="Think!";
    firstList[7]="Stay cool";
    firstList[8]="Money now";
    firstList[9]="Robbery";
    firstList[10]="This is a robbery";

    secondList= new Array ();

    secondList[0]="I have a gun";
    secondList[1]="Give me all of your $100s";
    secondList[2]="no dye packs";
    secondList[3]="give me $2000";
    secondList[4]="I will shoot you";
    secondList[5]="nobody will get hurt";
    secondList[6]="100s, 50s, 20s";
    secondList[7]="be quick";
    secondList[8]="put the money in the bag";
    secondList[9]="don't be stupid";
    secondList[10]="put it in the bag";

    thirdList= new Array ();

    thirdList[0]="do not delay.";
    thirdList[1]="hurry up.";
    thirdList[2]="no bills under $20.";
    thirdList[3]="do it now.";
    thirdList[4]="no one has to die.";
    thirdList[5]="no one has to know.";
    thirdList[6]="you have three minutes.";
    thirdList[7]="no tricks.";
    thirdList[8]="you have 15 seconds.";
    thirdList[9]="no bait, no alarm, no jokes.";
    thirdList[10]="Thanks.";

    //first random number
    now = new Date();
    seed = now.getSeconds();
    var rand1 = Math.round(Math.random(seed) * firstList.length);

    if (rand1 == firstList.length){rand1 = 0}

    //second random number

    var rand2 = Math.round(Math.random(seed) * secondList.length);
    if (rand2 == secondList.length){rand2 = 0}

    //third random number

    var rand3 = Math.round(Math.random(seed) * thirdList.length);
    if (rand3 == thirdList.length){rand3 = 0}


    var first = firstList[rand1];
    var second = secondList[rand2];
    var third = thirdList [rand3];

    // random number for lines
    var NumUnique = Math.floor(Math.random() * 3)+1;
    if (NumUnique == 1)
    {
    document.it.txt1.value = first;
    }
    else if (NumUnique == 2)
    {
    document.it.txt1.value = first + '\n' + second;
    }
    else if (NumUnique == 3)
    {
    document.it.txt1.value = first + '\n' + second + '\n' + third;
    }

    }

    </script>

    </head>

    <body>
    <p>
    </p>
    <input type="button" value="Go" onclick="getaNote()">
    <form name=it>
    <textarea wrap="virtual" id="txt1" rows="4" cols="30"style="overflow: auto;">
    generate a random bank note
    </textarea> <br/></form>


    </body>
    </html>


  •  

    Tags for this Thread

    Posting Permissions

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