Math.random()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Mlynarczyk

    Math.random()

    I remember there is a programming language where you can initialize the
    random number generator, so that it can - if you want - give you the exactly
    same sequence of random numbers every time you initialize it with the same
    parameter. Can this be done with JavaScript? I couldn't find anything in the
    documentation. Basically, what I want to achieve is to obtain always the
    same sequence of random numbers for the same given initialization value (but
    of course different sequences for different init values).

    Can this be done in JavaScript?

    Greetings,
    Thomas



  • lallous

    #2
    Re: Math.random()

    Hello

    AFAIK, This cannot be done w/ JavaScript, but allowed with languages such as
    C, Pascal, ...

    To solve your problem, write your own random and randomize functions as:

    var randSeed = 1234; // initial seed
    function randomize()
    {
    // use math.random() to generate a random number and assign to initial
    seed
    }

    function random()
    {
    randSeed = (randSeed * 1232) + (randSeed % 212) - randSeed & 0xFA11;
    // put any forumla you want....i am not an export at writing pseudo number
    generators functions
    }

    To generate same sequence:
    randSeed = 232; // your initial seed
    for (i=0;i<10;i++)
    document.write( random() + "<br>"); // this will generate same sequence!

    To generate a new sequence everytime, call randomize() or simply init
    randSeed variable to a value of your choice.

    HTH,
    Elias
    "Thomas Mlynarczyk" <blue_elephant5 5@hotmail.com> wrote in message
    news:c0qahd$e4v $03$1@news.t-online.com...[color=blue]
    > I remember there is a programming language where you can initialize the
    > random number generator, so that it can - if you want - give you the[/color]
    exactly[color=blue]
    > same sequence of random numbers every time you initialize it with the same
    > parameter. Can this be done with JavaScript? I couldn't find anything in[/color]
    the[color=blue]
    > documentation. Basically, what I want to achieve is to obtain always the
    > same sequence of random numbers for the same given initialization value[/color]
    (but[color=blue]
    > of course different sequences for different init values).
    >
    > Can this be done in JavaScript?
    >
    > Greetings,
    > Thomas
    >
    >
    >[/color]


    Comment

    • Thomas Mlynarczyk

      #3
      Re: Math.random()

      Also sprach lallous:
      [color=blue]
      > AFAIK, This cannot be done w/ JavaScript[/color]

      :-(((
      [color=blue]
      > randSeed = (randSeed * 1232) + (randSeed % 212) - randSeed & 0xFA11;
      > // put any forumla you want....i am not an export at writing pseudo
      > number generators functions[/color]

      Well, I don't need anything statistically sophisticated. The background is,
      I'm programming a game and need to lay out some cards in a random order to
      start playing. Now I want to be able to get back to the same layout later by
      simply selecting "game number 12345", where 12345 will be the seed that
      should always generate the same deck of cards. Currently I shuffle the cards
      by selecting two at random and swapping their places. This I do a number of
      times, so more or less all the cards should be randomly displaced.


      Comment

      • Dr John Stockton

        #4
        Re: Math.random()

        JRS: In article <c0qd9i$18d6n1$ 1@ID-161723.news.uni-berlin.de>, seen in
        news:comp.lang. javascript, lallous <lallous@lgwm.o rg> posted at Mon, 16
        Feb 2004 14:26:56 :-

        Responses should go after trimmed quotes, as per FAQ; corrected.
        [color=blue]
        >"Thomas Mlynarczyk" <blue_elephant5 5@hotmail.com> wrote in message
        >news:c0qahd$e4 v$03$1@news.t-online.com...[color=green]
        >> I remember there is a programming language where you can initialize the
        >> random number generator, so that it can - if you want - give you the[/color]
        >exactly[color=green]
        >> same sequence of random numbers every time you initialize it with the same
        >> parameter. Can this be done with JavaScript? I couldn't find anything in[/color]
        >the[color=green]
        >> documentation. Basically, what I want to achieve is to obtain always the
        >> same sequence of random numbers for the same given initialization value[/color]
        >(but[color=green]
        >> of course different sequences for different init values).
        >>
        >> Can this be done in JavaScript?[/color][/color]
        [color=blue]
        >AFAIK, This cannot be done w/ JavaScript, but allowed with languages such as
        >C, Pascal, ...[/color]

        It is not necessarily a matter of the language, but may be just of the
        implementation of the language library. If anyone here is writing a new
        ECMA-262, then the random seed needs to be made a read/write variable.
        In test, it is useful for randoms to be reproducible.


        [color=blue]
        >To solve your problem, write your own random and randomize functions as:
        >
        >var randSeed = 1234; // initial seed
        >function randomize()
        >{
        > // use math.random() to generate a random number and assign to initial
        >seed
        >}
        >
        >function random()
        >{
        > randSeed = (randSeed * 1232) + (randSeed % 212) - randSeed & 0xFA11;
        > // put any forumla you want....i am not an export at writing pseudo number
        >generators functions
        >}[/color]


        Nor an expert at spelling; but, from TZ +0200, that is not important.

        Donald E Knuth wrote :- "Random numbers should not be generated with a
        method chosen at random". Choice of a good algorithm is non-trivial,
        except for those willing to look up available resources rather than
        reinvent the wheel themselves.

        See
        <URL:http://www.merlyn.demo n.co.uk/pas-rand.htm>
        <URL:http://www.merlyn.demo n.co.uk/js-randm.htm>
        and Knuth's volumes.

        The following are reported good (X[n] are successive RandSeed) :-

        X[n+1] = 134775813*X[n] + 1 (mod 2^32)
        X[n+1] = 1664525*X[n] + 1013904223 (mod 2^32)

        Divide by 2^32, of course.

        The expression of lallous is, in fact, remarkably bad; initialised with
        1234, it immediately enters a cycle of length 4. With 1, it soon
        reaches a cycle of length 2.

        0xFA11 = 1111 1010 0001 0001, with 8 bits set; we have, in effect, an
        8-bit seed and a maximum cycle length of 256.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 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> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        • lallous

          #5
          Re: Math.random()

          Dr John Stockton <spam@merlyn.de mon.co.uk> wrote in message news:<jKwUe6FaN SMAFwfL@merlyn. demon.co.uk>...[color=blue]
          > JRS: In article <c0qd9i$18d6n1$ 1@ID-161723.news.uni-berlin.de>, seen in
          > news:comp.lang. javascript, lallous <lallous@lgwm.o rg> posted at Mon, 16
          > Feb 2004 14:26:56 :-
          >
          > Responses should go after trimmed quotes, as per FAQ; corrected.
          >[color=green]
          > >"Thomas Mlynarczyk" <blue_elephant5 5@hotmail.com> wrote in message
          > >news:c0qahd$e4 v$03$1@news.t-online.com...[color=darkred]
          > >> I remember there is a programming language where you can initialize the
          > >> random number generator, so that it can - if you want - give you the[/color][/color]
          > exactly[color=green][color=darkred]
          > >> same sequence of random numbers every time you initialize it with the same
          > >> parameter. Can this be done with JavaScript? I couldn't find anything in[/color][/color]
          > the[color=green][color=darkred]
          > >> documentation. Basically, what I want to achieve is to obtain always the
          > >> same sequence of random numbers for the same given initialization value[/color][/color]
          > (but[color=green][color=darkred]
          > >> of course different sequences for different init values).
          > >>
          > >> Can this be done in JavaScript?[/color][/color]
          >[color=green]
          > >AFAIK, This cannot be done w/ JavaScript, but allowed with languages such as
          > >C, Pascal, ...[/color]
          >
          > It is not necessarily a matter of the language, but may be just of the
          > implementation of the language library. If anyone here is writing a new
          > ECMA-262, then the random seed needs to be made a read/write variable.
          > In test, it is useful for randoms to be reproducible.
          >
          >
          >[color=green]
          > >To solve your problem, write your own random and randomize functions as:
          > >
          > >var randSeed = 1234; // initial seed
          > >function randomize()
          > >{
          > > // use math.random() to generate a random number and assign to initial
          > >seed
          > >}
          > >
          > >function random()
          > >{
          > > randSeed = (randSeed * 1232) + (randSeed % 212) - randSeed & 0xFA11;
          > > // put any forumla you want....i am not an export at writing pseudo number
          > >generators functions
          > >}[/color]
          >
          >
          > Nor an expert at spelling; but, from TZ +0200, that is not important.[/color]
          I learned my english through programming...s o no wonder if tech words
          come to mind and fingers first! ;)
          [color=blue]
          >
          > Donald E Knuth wrote :- "Random numbers should not be generated with a
          > method chosen at random". Choice of a good algorithm is non-trivial,
          > except for those willing to look up available resources rather than
          > reinvent the wheel themselves.
          >
          > See
          > <URL:http://www.merlyn.demo n.co.uk/pas-rand.htm>
          > <URL:http://www.merlyn.demo n.co.uk/js-randm.htm>
          > and Knuth's volumes.
          >
          > The following are reported good (X[n] are successive RandSeed) :-
          >
          > X[n+1] = 134775813*X[n] + 1 (mod 2^32)
          > X[n+1] = 1664525*X[n] + 1013904223 (mod 2^32)
          >
          > Divide by 2^32, of course.
          >
          > The expression of lallous is, in fact, remarkably bad; initialised with
          > 1234, it immediately enters a cycle of length 4. With 1, it soon
          > reaches a cycle of length 2.[/color]
          Yes, I am aware of that, I was just trying to indicate how to create
          own random function with a seed variable.
          [color=blue]
          >
          > 0xFA11 = 1111 1010 0001 0001, with 8 bits set; we have, in effect, an
          > 8-bit seed and a maximum cycle length of 256.[/color]

          --
          Elias

          Comment

          • Thomas Mlynarczyk

            #6
            Re: Math.random()

            Strange... I had posted this before, but it doesn't seem to show up...? So,
            I'll have another try:

            Also sprach lallous:
            [color=blue]
            > AFAIK, This cannot be done w/ JavaScript
            > randSeed = (randSeed * 1232) + (randSeed % 212) - randSeed & 0xFA11;
            > // put any forumla you want....i am not an export at writing pseudo
            > number generators functions[/color]

            Well, I don't need anything statistically sophisticated. The background is,
            I'm programming a game and need to lay out some cards in a random order to
            start playing. Now I want to be able to get back to the same layout later by
            simply selecting "game number 12345", where 12345 will be the seed that
            should always generate the same deck of cards. Currently I shuffle the cards
            by selecting two at random and swapping their places. This I do a number of
            times, so more or less all the cards should be randomly displaced.


            Comment

            • Dr John Stockton

              #7
              Re: Math.random()

              JRS: In article <c0r8gf$fsb$01$ 1@news.t-online.com>, seen in
              news:comp.lang. javascript, Thomas Mlynarczyk
              <blue_elephant5 5@hotmail.com> posted at Mon, 16 Feb 2004 21:16:45 :-[color=blue]
              >Also sprach lallous:[/color]
              [color=blue]
              >Well, I don't need anything statistically sophisticated. The background is,
              >I'm programming a game and need to lay out some cards in a random order to
              >start playing. Now I want to be able to get back to the same layout later by
              >simply selecting "game number 12345", where 12345 will be the seed that
              >should always generate the same deck of cards. Currently I shuffle the cards
              >by selecting two at random and swapping their places. This I do a number of
              >times, so more or less all the cards should be randomly displaced.[/color]


              Your shuffling is, therefore, very probably not enough to achieve full
              randomness, or more than is necessary to achieve full randomness, or
              does not give equal probability to all possible results (assuming, that
              is, a perfect Random function).

              By reading the FAQ and following its "shuffling" reference, you could
              have found

              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 }

              which is AFAICS the best possible Shuffle.

              However, for your stated purpose, you do not need a Shuffle, but can do
              a Deal which generates 0..N-1 in random order

              function Deal(N) { var J, K, Q = new Array(N)
              for (J=0; J<N; J++) { K = Random(J+1) ; Q[J] = Q[K] ; Q[K] = J }
              return Q }

              The best choice depends on how you represent the cards by variables.

              --
              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 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> jscr maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

              Comment

              • John M. Gamble

                #8
                Re: Math.random()

                In article <cHMLdJGqOfMAFw pb@merlyn.demon .co.uk>,
                Dr John Stockton <spam@merlyn.de mon.co.uk> wrote:[color=blue]
                >JRS: In article <c0r8gf$fsb$01$ 1@news.t-online.com>, seen in
                >news:comp.lang .javascript, Thomas Mlynarczyk
                ><blue_elephant 55@hotmail.com> posted at Mon, 16 Feb 2004 21:16:45 :-[color=green]
                >>Also sprach lallous:[/color]
                >[color=green]
                >>Well, I don't need anything statistically sophisticated. The background is,
                >>I'm programming a game and need to lay out some cards in a random order to
                >>start playing. Now I want to be able to get back to the same layout later by
                >>simply selecting "game number 12345", where 12345 will be the seed that
                >>should always generate the same deck of cards. Currently I shuffle the cards
                >>by selecting two at random and swapping their places. This I do a number of
                >>times, so more or less all the cards should be randomly displaced.[/color]
                >
                >
                >Your shuffling is, therefore, very probably not enough to achieve full
                >randomness, or more than is necessary to achieve full randomness, or
                >does not give equal probability to all possible results (assuming, that
                >is, a perfect Random function).
                >
                >By reading the FAQ and following its "shuffling" reference, you could
                >have found
                >
                > 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 }
                >
                >which is AFAICS the best possible Shuffle.[/color]

                This is actually well-known to be a bad shuffle algorithm. The
                question comes up occasionally in sci.crypt, and also was discussed
                in detail in the comp.lang.perl. moderated newsgroup. I think that
                the perl FAQ was corrected with a much better shuffle as a result
                (look for the key word 'permute').

                The major flaw with this algorithm is that not all permutations are
                equally possible. This may not be the worst problem with a
                card-shuffling program, but i would be annoyed with it.

                The algorithm (or one of them) that should be looked at is the
                Fischer-Krause algorithm.

                --
                -john

                February 28 1997: Last day libraries could order catalogue cards
                from the Library of Congress.

                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: Math.random()

                  jgamble@ripco.c om (John M. Gamble) writes:
                  [color=blue]
                  > In article <cHMLdJGqOfMAFw pb@merlyn.demon .co.uk>,
                  > Dr John Stockton <spam@merlyn.de mon.co.uk> wrote:[/color]
                  [color=blue][color=green]
                  >>By reading the FAQ and following its "shuffling" reference, you could
                  >>have found
                  >>
                  >> 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 }
                  >>
                  >>which is AFAICS the best possible Shuffle.[/color]
                  >
                  > This is actually well-known to be a bad shuffle algorithm.[/color]
                  ....[color=blue]
                  > The major flaw with this algorithm is that not all permutations are
                  > equally possible. This may not be the worst problem with a
                  > card-shuffling program, but i would be annoyed with it.[/color]

                  I think you are misreading the algorithm. In particular, notice that
                  the call to Random uses the loop iterator as argument. It does perform
                  a permutation and all permutations are equally probable (if the Random
                  function is, at least ... there are small deviations because you can't
                  use a random number in the range, e.g., 0..2^32-1 to pick a random
                  number in the range 0..6 with equal probability, but that is not a
                  serious problem unless Q.length is very large)

                  /L
                  --
                  Lasse Reichstein Nielsen - lrn@hotpop.com
                  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                  'Faith without judgement merely degrades the spirit divine.'

                  Comment

                  • Pete

                    #10
                    Re: Math.random()

                    "Thomas Mlynarczyk" <blue_elephant5 5@hotmail.com> wrote in message news:<c0r8gf$fs b$01$1@news.t-online.com>...[color=blue]
                    > Also sprach lallous:
                    >[color=green]
                    > > AFAIK, This cannot be done w/ JavaScript[/color]
                    >
                    > :-(((
                    >[color=green]
                    > > randSeed = (randSeed * 1232) + (randSeed % 212) - randSeed & 0xFA11;
                    > > // put any forumla you want....i am not an export at writing pseudo
                    > > number generators functions[/color]
                    >
                    > Well, I don't need anything statistically sophisticated. The background is,
                    > I'm programming a game and need to lay out some cards in a random order to
                    > start playing. Now I want to be able to get back to the same layout later by
                    > simply selecting "game number 12345", where 12345 will be the seed that
                    > should always generate the same deck of cards. Currently I shuffle the cards
                    > by selecting two at random and swapping their places. This I do a number of
                    > times, so more or less all the cards should be randomly displaced.[/color]

                    This is similar to Freecells game recall method:

                    <html>
                    <head>
                    <title>Game Id</title>
                    </head>
                    <body>

                    <script>
                    var gameNumber=1; //1 to 1,000,000
                    var n=52; //card numbers, id's or whatever.

                    function rnd(){
                    gameNumber=game Number*314591+3 43421;
                    gameNumber=game Number-1000000*Math.fl oor(gameNumber/1000000);
                    return gameNumber/1000000;
                    }

                    function doIt(){
                    chosenGame=new Array();
                    for (var i=0; i < n; i++){
                    chosenGame[i] = i;
                    }
                    var k, x;
                    for (var i=0; i < n-1; i++){
                    k=Math.floor((n-i)*rnd());
                    if (k==(n - i)){
                    k=n-i-1;
                    }
                    x=chosenGame[i];
                    chosenGame[i]=chosenGame[i+k];
                    chosenGame[i+k]=x;
                    }
                    alert("Game id = "+gameNumber+"\ nHowever, your user would just"
                    +" input original 'gameNumber' to replay this game, #1 in this"
                    +" example.\n\n"+c hosenGame);
                    }
                    doIt();
                    </script>

                    </body>
                    </html>

                    Comment

                    • Thomas Mlynarczyk

                      #11
                      Re: Math.random()

                      Also sprach Lasse Reichstein Nielsen:
                      [color=blue][color=green][color=darkred]
                      >>> By reading the FAQ and following its "shuffling" reference, you
                      >>> could have found
                      >>>
                      >>> 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 }
                      >>>
                      >>> which is AFAICS the best possible Shuffle.[/color][/color][/color]
                      [color=blue][color=green]
                      >> This is actually well-known to be a bad shuffle algorithm.[/color][/color]
                      [color=blue]
                      > I think you are misreading the algorithm.[/color]

                      Thanks to all of you for your advice and pointing out that link in the FAQ.
                      The algorithm that I am currently using is this:

                      var i = 2 * cards.length;
                      while(i--) {
                      c1 = parseInt(Math.r andom() * cards.length);
                      c2 = parseInt(Math.r andom() * cards.length);
                      c = cards[c1];
                      cards[c1] = cards[c2];
                      cards[c2] = c;
                      }

                      I'm sure it could be optimized and maybe a lower initial value for i would
                      be sufficient.



                      Comment

                      • Thomas Mlynarczyk

                        #12
                        Re: Math.random()

                        Also sprach Pete:
                        [color=blue]
                        > This is similar to Freecells game recall method:[/color]
                        [color=blue]
                        > var gameNumber=1; file://1 to 1,000,000
                        > var n=52; file://card numbers, id's or whatever.
                        >
                        > function rnd(){
                        > gameNumber=game Number*314591+3 43421;
                        > gameNumber=game Number-1000000*Math.fl oor(gameNumber/1000000);
                        > return gameNumber/1000000;
                        > }
                        >
                        > function doIt(){
                        > chosenGame=new Array();
                        > for (var i=0; i < n; i++){
                        > chosenGame[i] = i;
                        > }
                        > var k, x;
                        > for (var i=0; i < n-1; i++){
                        > k=Math.floor((n-i)*rnd());
                        > if (k==(n - i)){
                        > k=n-i-1;
                        > }
                        > x=chosenGame[i];
                        > chosenGame[i]=chosenGame[i+k];
                        > chosenGame[i+k]=x;
                        > }
                        > alert("Game id = "+gameNumber+"\ nHowever, your user would just"
                        > +" input original 'gameNumber' to replay this game, #1 in this"
                        > +" example.\n\n"+c hosenGame);
                        > }
                        > doIt();[/color]

                        Thanks a lot - that seems to be the thing I'm looking for! :-)

                        Greetings,
                        Thomas



                        Comment

                        • Dr John Stockton

                          #13
                          Re: Math.random()

                          JRS: In article <1abcadcc.04021 72010.332138fc@ posting.google. com>, seen
                          in news:comp.lang. javascript, Pete <someonesgotmyn ame2002@yahoo.c o.uk>
                          posted at Tue, 17 Feb 2004 20:10:43 :-

                          [color=blue]
                          >This is similar to Freecells game recall method:[/color]

                          [color=blue]
                          >var gameNumber=1; //1 to 1,000,000[/color]

                          I think that should be 0 to 999999
                          [color=blue]
                          >var n=52; //card numbers, id's or whatever.
                          >
                          >function rnd(){
                          > gameNumber=game Number*314591+3 43421;
                          > gameNumber=game Number-1000000*Math.fl oor(gameNumber/1000000);[/color]

                          That looks like gameNumber %= 1000000
                          [color=blue]
                          > return gameNumber/1000000;
                          >}[/color]

                          So that's a mod 10^6 version of the usual mod 2^32 or 2^n algorithm. I
                          expect it to be good if the numbers are well-chosen, but not if they are
                          randomly chosen. In Math.random, the method should be at least equally
                          good, but implemented faster.


                          [color=blue]
                          > for (var i=0; i < n; i++){
                          > chosenGame[i] = i;
                          > }
                          > var k, x;
                          > for (var i=0; i < n-1; i++){
                          > k=Math.floor((n-i)*rnd());
                          > if (k==(n - i)){
                          > k=n-i-1;
                          > }
                          > x=chosenGame[i];
                          > chosenGame[i]=chosenGame[i+k];
                          > chosenGame[i+k]=x;
                          >}[/color]

                          That looks similar to mine, but less efficient in its indexing.

                          The if (k==(n - i)){ k=n-i-1; } should not be necessary, since the
                          result of rnd seems to be less than 1.


                          "My" method was taken from a reliable-seeming source, and most people
                          seem willing to believe that it is best.

                          Discussion is at <URL:http://www.merlyn.demo n.co.uk/pas-rand.htm#SDD>.

                          --
                          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
                          <URL:http://www.merlyn.demo n.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
                          <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

                          Comment

                          • Dr John Stockton

                            #14
                            Re: Math.random()

                            JRS: In article <c0ttmb$c62$1@e 250.ripco.com>, seen in
                            news:comp.lang. javascript, John M. Gamble <jgamble@ripco. com> posted at
                            Tue, 17 Feb 2004 20:31:07 :-[color=blue][color=green]
                            >>By reading the FAQ and following its "shuffling" reference, you could
                            >>have found
                            >>
                            >> 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 }
                            >>
                            >>which is AFAICS the best possible Shuffle.[/color]
                            >
                            >This is actually well-known to be a bad shuffle algorithm.[/color]

                            I don't believe you.

                            [color=blue]
                            > The
                            >question comes up occasionally in sci.crypt, and also was discussed
                            >in detail in the comp.lang.perl. moderated newsgroup. I think that
                            >the perl FAQ was corrected with a much better shuffle as a result
                            >(look for the key word 'permute').[/color]

                            I, like many others here, am a dial-up off-line user; so, where known,
                            "Please Give URL". The algorithm in the sci.crypto FAQ seems to be
                            equivalent to the above, encoded in C.

                            [color=blue]
                            >The major flaw with this algorithm is that not all permutations are
                            >equally possible.[/color]

                            I certainly don't believe that.
                            [color=blue]
                            > This may not be the worst problem with a
                            >card-shuffling program, but i would be annoyed with it.[/color]

                            Agreed that it would be a major flaw.
                            [color=blue]
                            >The algorithm (or one of them) that should be looked at is the
                            >Fischer-Krause algorithm.[/color]

                            PGU.


                            --
                            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 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> jscr maths, dates, sources.
                            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                            Comment

                            • Lasse Reichstein Nielsen

                              #15
                              Re: Math.random()

                              Dr John Stockton <spam@merlyn.de mon.co.uk> writes:
                              [color=blue]
                              > "My" method was taken from a reliable-seeming source, and most people
                              > seem willing to believe that it is best.[/color]

                              Knuth's "The Art of Computer Programming" is not just reliable-seeming.
                              It is authoritative, going on definitive. :)

                              /L
                              --
                              Lasse Reichstein Nielsen - lrn@hotpop.com
                              DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                              'Faith without judgement merely degrades the spirit divine.'

                              Comment

                              Working...