random text script, BUT no repeating

Discussion in 'Javascript' started by Jennie Friesen, Jun 30, 2003.

  1. Hello--

    I would like to display a different line of text (32 different
    messages) on refresh, BUT with no repeats.

    The script I am currently using is:

    <script language=javascript type=text/javascript>
    <!--
    a = Math.floor(Math.random() * 32);
    switch (a){
    case 0 :
    document.write("msg 1 here");
    break;
    case 1 :
    document.write("msg 2 here");
    break;
    }
    // etc., up to 32 cases
    //-->
    </script>

    I notice that many random text scripts use an array to store the text
    messages, and was thinking that incorporating a line in the script
    where the array value that was most recently accessed would be
    deleted, would make its subsequent selection impossible, thereby
    solving the no-repeats.

    In the script below, can one suggest where the delete line would fit
    in and what its syntax would be?

    thank you in advance!
    JF

    <SCRIPT LANGUAGE="Javascript">
    // <!--
    function text() {
    };

    text = new text();
    number = 0;

    // textArray
    text[number++] = "Random text string 1."
    text[number++] = "Random text string 2."
    text[number++] = "Random text string 3."
    text[number++] = "Random text string 4."
    text[number++] = "Random text string 5."
    // keep adding items here...

    increment = Math.floor(Math.random() * number);

    document.write(text[increment]);

    // where does delete text[number++] fit in?

    //--></SCRIPT>
     
    Jennie Friesen, Jun 30, 2003
    #1
    1. Advertisements

  2. Jennie Friesen

    Dan Brussee Guest

    Think of this is a sort of card shuffling routine. You have 32 cards and
    want them randomized. Obviously, not duplicates.

    A common way of doing this in gaming is to take the array of cards and
    just mix up their values. Then go through the array one at a time as if
    they were NOT shuffled. Psuedo-code follows:

    var a;
    var b;
    var tmp;
    for (var y = 0; y < 10; y++) { // repeat shuffle y times
    for (var x = 0; x < 32; x++) {
    a = math.random(32); // cant remember the method here... sorry.
    b = math.random(32);
    tmp = ary(a);
    ary(a) = ary(b);
    ary(b) = tmp;
    }
    }

    for (var x = 0; x < 32; x++) {
    document.write("Item " + x + ": " + ary(x) + "<br/>");
    }
     
    Dan Brussee, Jun 30, 2003
    #2
    1. Advertisements

  3. Jennie Friesen

    Tim Williams Guest

    You want to do this every time the page is refreshed?
    Unless this is a framed page, it's not clear how the page will know what the
    last-used line was...

    tim.
     
    Tim Williams, Jun 30, 2003
    #3
  4. JRS: In article <>,
    seen in Dan Brussee <[email protected]
    puting.com> posted at Mon, 30 Jun 2003 01:06:41 :-
    Pseudo.

    Good idea, bad algorithm. See FAQ 4.22, both for
    function Random(x) { return Math.floor(x*Math.random()) }
    // generates random integer in 0 .. x-1
    and for a link to efficient dealing and shuffling.

    For N items, there are N! possible orders, so for a good shuffle the
    operation must have [a multiple of] N! possible outcomes, all equi-
    probable. For this it is both necessary and sufficient to call
    Random(N) ... Random(2 or 1).

    With, therefore, a little subtlety in your x loop, the y loop is not
    needed.

    function Shuffle(Q) { var R, T, J
    for (J=Q.length-1 ; J>0 ; J--)
    { R=Random(J+1) ; T=Q[J] ; Q[J]=Q[R] ; Q[R]=T }
    return Q }
     
    Dr John Stockton, Jun 30, 2003
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.