By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,665 Members | 1,952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,665 IT Pros & Developers. It's quick & easy.

Netscape 4.8 Math Random Hangup

P: n/a
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='Javascript1.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.writeln(c);}
</SCRIPT >
</HEAD>

<BODY>
<H2>Example</H2>
<FORM>
<INPUT TYPE="button" NAME="Example" onClick="count(75)">
</FORM>
</BODY>
</HTML>
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 03.7.19 9:53 PM, R. Russell Kinter wrote:
Hi all,
First of all I am fairly new to javascript. Most of my experience has
been with
its subset vrmlscript, so have mercy.
I'll try ;-)
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.
(I don't see how this will make the numbers any more random.)
var d = new Array;


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.writeln(c);
}
Phil

--
Philip Ronan
ph***********@virgin.net
(Please remove the "z"s if replying by email)
Jul 20 '05 #2

P: n/a
In article <1a**************************@posting.google.com >, Py***@comcast.net
(R. Russell Kinter) writes:

<--snip-->
The following script works in I.E. 5.5, but hangs up in Netscape 4.8
IE forgives syntax errors a lot easier than NN4.8 does. new Array; is a syntax
error. It should be new Array();
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?
Fix the syntax error new Array();

<--snip-->
<SCRIPT language='Javascript1.2'>
Language is deprecated in favor of type="text/javascript"

Also, setting the language to javascript1.2 can cause some strange behavior in
NN4.xx.

function count(a){
var c = 0;
var d = new Array;
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.writeln(c);}


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.
Jul 20 '05 #3

P: n/a
Lee
Py***@comcast.net said:

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='Javascript1.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.writeln(c);}
</SCRIPT >
</HEAD>

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

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

Jul 20 '05 #4

P: n/a
Russ Kinter <py***@comcast.net> writes:
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.
By adding a few more complex steps?
Eventually the complex series of steps of Math.random() starts over
again right?
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.
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.
If it was a problem.
So yeah, I think it is more random, or is my understanding
of Math.random() possibly not so good?


I think you underestimate its unpredictability.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

P: n/a
JRS: In article <1a**************************@posting.google.com >, seen
in news:comp.lang.javascript, R. Russell Kinter <Py***@comcast.net>
posted at Sat, 19 Jul 2003 13:53:52 :-
The general idea is to make Math.random() more "random" by calling it
x number
of times.
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.

...
d[b] = random();
if(b==a){c = round(77*d[b]);}}}
document.writeln(c);}


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.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.