Netscape 4.8 Math Random Hangup

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • R. Russell Kinter

    Netscape 4.8 Math Random Hangup

    Hi all,
    First of all I am fairly new to javascript. Most of my experience has
    been with
    its subset vrmlscript, so have mercy.
    The following script works in I.E. 5.5, but hangs up in Netscape 4.8
    (also works in vrmlscript for blaxxun Contact vrml plugin)
    The general idea is to make Math.random() more "random" by calling it
    x number
    of times. X would would be decimal places taken from a time stamp, but
    in this simplified example it is just 75.
    Any ideas on how to keep it from hanging in Netscape?

    thanx
    Russ Kinter



    <HTML>

    <HEAD>

    <SCRIPT language='Javas cript1.2'>


    function count(a){
    var c = 0;
    var d = new Array;
    with (Math){
    for(var b = 0;b<=a;b++){ d[b] = random();
    if(b==a){c = round(77*d[b]);}}}
    document.writel n(c);}


    </SCRIPT >
    </HEAD>

    <BODY>
    <H2>Example</H2>
    <FORM>
    <INPUT TYPE="button" NAME="Example" onClick="count( 75)">
    </FORM>
    </BODY>
    </HTML>
  • Philip Ronan

    #2
    Re: Netscape 4.8 Math Random Hangup

    On 03.7.19 9:53 PM, R. Russell Kinter wrote:
    [color=blue]
    > Hi all,
    > First of all I am fairly new to javascript. Most of my experience has
    > been with
    > its subset vrmlscript, so have mercy.[/color]

    I'll try ;-)
    [color=blue]
    > The following script works in I.E. 5.5, but hangs up in Netscape 4.8
    > (also works in vrmlscript for blaxxun Contact vrml plugin)
    > The general idea is to make Math.random() more "random" by calling it
    > x number
    > of times.[/color]

    (I don't see how this will make the numbers any more random.)
    [color=blue]
    > var d = new Array;[/color]

    I think the standard syntax is "new Array()" with the round brackets.
    But why do you need an array anyway? You aren't using any values from it
    apart from the last one.

    This should give you the same results:

    function count(a) {
    var c = 0;
    for (var b=0; b<=a; b++) c = Math.random();
    c = Math.round(77*c );
    document.writel n(c);
    }


    Phil

    --
    Philip Ronan
    phil.ronanzzz@v irgin.net
    (Please remove the "z"s if replying by email)


    Comment

    • HikksNotAtHome

      #3
      Re: Netscape 4.8 Math Random Hangup

      In article <1a90d129.03071 91253.652ee88c@ posting.google. com>, Pyth7@comcast.n et
      (R. Russell Kinter) writes:

      <--snip-->
      [color=blue]
      >The following script works in I.E. 5.5, but hangs up in Netscape 4.8[/color]

      IE forgives syntax errors a lot easier than NN4.8 does. new Array; is a syntax
      error. It should be new Array();
      [color=blue]
      >The general idea is to make Math.random() more "random" by calling it
      >x number of times. X would would be decimal places taken from a time
      >stamp, but in this simplified example it is just 75.
      >Any ideas on how to keep it from hanging in Netscape?[/color]

      Fix the syntax error new Array();

      <--snip-->
      [color=blue]
      > <SCRIPT language='Javas cript1.2'>[/color]

      Language is deprecated in favor of type="text/javascript"

      Also, setting the language to javascript1.2 can cause some strange behavior in
      NN4.xx.
      [color=blue]
      >
      >function count(a){
      >var c = 0;
      >var d = new Array;[/color]

      var d = new Array();
      [color=blue]
      >with (Math){
      >for(var b = 0;b<=a;b++){ d[b] = random();
      >if(b==a){c = round(77*d[b]);}}}
      >document.write ln(c);}[/color]

      Adding the () to the array constructor allows it to work in NN4.80
      --
      Randy
      All code posted is dependent upon the viewing browser
      supporting the methods called, and Javascript being enabled.

      Comment

      • Lee

        #4
        Re: Netscape 4.8 Math Random Hangup

        Pyth7@comcast.n et said:[color=blue]
        >
        >Hi all,
        >First of all I am fairly new to javascript. Most of my experience has
        >been with
        >its subset vrmlscript, so have mercy.
        >The following script works in I.E. 5.5, but hangs up in Netscape 4.8
        >(also works in vrmlscript for blaxxun Contact vrml plugin)
        >The general idea is to make Math.random() more "random" by calling it
        >x number
        >of times. X would would be decimal places taken from a time stamp, but
        >in this simplified example it is just 75.
        >Any ideas on how to keep it from hanging in Netscape?
        >
        >thanx
        >Russ Kinter
        >
        >
        >
        ><HTML>
        >
        ><HEAD>
        >
        > <SCRIPT language='Javas cript1.2'>
        >
        >
        >function count(a){
        >var c = 0;
        >var d = new Array;
        >with (Math){
        >for(var b = 0;b<=a;b++){ d[b] = random();
        >if(b==a){c = round(77*d[b]);}}}
        >document.write ln(c);}
        >
        >
        ></SCRIPT >
        ></HEAD>
        >
        ><BODY>
        ><H2>Example</H2>
        ><FORM>
        ><INPUT TYPE="button" NAME="Example" onClick="count( 75)">
        > </FORM>
        ></BODY>
        ></HTML>[/color]


        Decimal places taken from a timestamp won't make it any
        more random, particularly if you're going to skew the
        distribution by rounding the result. Rounding ensures
        that the first and last possibility are chosen half as
        often as any other value.

        Your loop ends when a is greater than b, but you seem
        to want the condition that a==b, so you should just
        stop your loop one step earlier.

        You don't need an array unless you're going to use the
        values you assign to its elements.

        There's no need to assign c=0 if you're going to assign
        another value to it.

        function count(a){
        var c;
        while(a--){
        c=Math.random() ;
        }
        document.write( Math.floor(c*77 )+1));
        }

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Netscape 4.8 Math Random Hangup

          Russ Kinter <pyth7@comcast. net> writes:
          [color=blue]
          > However I would like explain my idea about increasing the "randomness " a
          > little more. Since Math.random() really isn't random itself but a
          > complex series of steps that tries to "fake" it, then I think my method
          > does improve a little on it.[/color]

          By adding a few more complex steps?
          [color=blue]
          > Eventually the complex series of steps of Math.random() starts over
          > again right?[/color]

          The period of a typical pseudo-random generator used in Javascript
          implementations is often about 4 billion, so you are not likely to
          have problems due to the sequence starting to repeat.
          [color=blue]
          > Then by using the decimal of a timestamp ( say 5 ), and calling
          > Math.random() 5 times you would get 16 rather than
          > relying on the ordinary one by one sequence where you would get 3.[/color]

          If it was a problem.
          [color=blue]
          > So yeah, I think it is more random, or is my understanding
          > of Math.random() possibly not so good?[/color]

          I think you underestimate its unpredictabilit y.
          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Dr John Stockton

            #6
            Re: Netscape 4.8 Math Random Hangup

            JRS: In article <1a90d129.03071 91253.652ee88c@ posting.google. com>, seen
            in news:comp.lang. javascript, R. Russell Kinter <Pyth7@comcast. net>
            posted at Sat, 19 Jul 2003 13:53:52 :-
            [color=blue]
            >The general idea is to make Math.random() more "random" by calling it
            >x number
            >of times.[/color]

            That's pointless, unless you are one of the rare people whose use of
            random is sensitive to the means by which it is generated.

            [color=blue]
            > ...
            > d[b] = random();
            >if(b==a){c = round(77*d[b]);}}}
            >document.write ln(c);}[/color]

            0.0 <= d[b] < 1.0 // <= 1.0 in bad systems

            Your c will have half a chance of being 0, half a chance of being 77,
            and one chance of being each of 1 to 76. Integers chosen at random
            should be evenly distributed over their range.

            Read the FAQ.

            Code should be indented to show structure.

            --
            © 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> JS maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

            Comment

            Working...