random text script, BUT no repeating

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jennie Friesen

    random text script, BUT no repeating

    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=javasc ript 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="Javas cript">
    // <!--
    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>
  • Dan Brussee

    #2
    Re: random text script, BUT no repeating

    In article <bef3c3af.03062 91642.25211147@ posting.google. com>,
    jenniefriesen@y ahoo.ca says...[color=blue]
    > Hello--
    >
    > I would like to display a different line of text (32 different
    > messages) on refresh, BUT with no repeats.[/color]

    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/>");
    }

    --

    Remove NOT from email address to reply. AntiSpam in action.

    Comment

    • Tim Williams

      #3
      Re: random text script, BUT no repeating

      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.




      "Jennie Friesen" <jenniefriesen@ yahoo.ca> wrote in message
      news:bef3c3af.0 306291642.25211 147@posting.goo gle.com...[color=blue]
      > 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=javasc ript 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="Javas cript">
      > // <!--
      > 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>[/color]


      Comment

      • Dr John Stockton

        #4
        Re: random text script, BUT no repeating

        JRS: In article <MPG.1969572b7c 91c646989781@ne ws-server.nc.rr.co m>,
        seen in news:comp.lang. javascript, Dan Brussee <dbrussee@NOTbe tterwaycom
        puting.com> posted at Mon, 30 Jun 2003 01:06:41 :-[color=blue]
        >In article <bef3c3af.03062 91642.25211147@ posting.google. com>,
        >jenniefriesen@ yahoo.ca says...[color=green]
        >> Hello--
        >>
        >> I would like to display a different line of text (32 different
        >> messages) on refresh, BUT with no repeats.[/color]
        >
        >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/>");
        >}[/color]

        Pseudo.

        Good idea, bad algorithm. See FAQ 4.22, both for
        function Random(x) { return Math.floor(x*Ma th.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 }

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MSIE 4 ©
        <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

        Comment

        Working...