...

View Full Version : Math.random problem



student610
01-17-2004, 02:11 AM
I have read the tutorials about using random quotes and read sample scripts but I still don't understand. This is what I have:
<HTML>
<HEAD>
<TITLE>The Mark Twain Quotes Page</TITLE>

<SCRIPT LANGUAGE="JavaScript">
<!--- Hide from older browsers
function MQuote (Qnum) {
var Quote=new Array();
Quote[0]='"I smoke in moderation, only one cigar at a time."';
Quote[1]='"Be careful of reading health books, you might die of a misprint."';
Quote[2]='"Man is the only animal that blushes--or needs to."';
Quote[3]='"Clothes make the man. Naked people have little or no influence on society."';
Quote[4]='"One of the most striking differences between a cat and a lie is that a cat has only nine lives."';
return Quote[Qnum];
}
function RandInt(Rnum) {
var Rnum=Math.round(Math.random()*5);
return Value[Rnum];
}
//Stop hiding -->
</SCRIPT>
</HEAD>

<BODY TEXT="#000000" LINKS="#00FF00" BACKGROUND="Paper.gif">
<TABLE WIDTH=600 BORDER=0 CELLPADDING=5 CELLSPACING=0>

<!---Random Mark Twain Quote --->
<TR><TD BGCOLOR="#FFFFCC" COLSPAN=2 ALIGN=CENTER>
<FONT COLOR=BROWN FACE="Arial, Helvetica, Sans Serif" SIZE=2>
<SCRIPT>
<!--- Hide from older browsers
var RandValue=RandInt;
document.write(Quote);
//Stop hiding -->
</SCRIPT>
</FONT>
</TD></TR>

<TR>
<!--- Mark Twain graphic --->
<TD VALIGN=TOP ALIGN=CENTER WIDTH=230>
<IMG SRC="Twain.gif" WIDTH=200 HEIGHT=302 ALT="Mark Twain"><BR>
</TD>

<!--- Page title --->
<TD WIDTH=370 ROWSPAN=2 VALIGN=TOP>
<IMG SRC="Logo.gif">

<!--- Page description --->
<P>Mark Twain is one of our most commonly quoted authors. The words of
Mark Twain have enlivened speeches for the past century. Many a candidate
running for higher office has skewered the incumbent by quoting Twain:
"... that there is no distinctly native American criminal class except
Congress."</P>
<P>This page is dedicated to the collected works of Mark Twain, with a
special emphasis on his quotes and maxims. You can also look for
additional Mark Twain quotes at the sites listed below:</P>

<!--- Links to other Web pages --->
<IMG SRC="Button.gif"><A HREF="http://www.tarleton.edu/~schmidt/Mark_Twain.html">Mark Twain Quotations</A><BR>
<IMG SRC="Button.gif"><A HREF="http://salwen.com/mtquotes.html">The Quotable Mark Twain</A><BR>
<IMG SRC="Button.gif"><A HREF="http://www.geocities.com/CapitolHill/8156/Twain.html">Mark Twain Daily Quote</A><BR>
<IMG SRC="Button.gif"><A HREF="http://www.lib.berkeley.edu/BANC/MTP/">The Mark Twain Project</A><BR>
<IMG SRC="Button.gif"><A HREF="http://marktwain.miningco.com/library/weekly/aa080597.htm">Maxims and Misattributed Quotes</A><BR>
</TD>
</TR>
</TABLE>

<BR>
<HR>
<SCRIPT>
<!--- Hide from older browsers

//Stop hiding -->
</SCRIPT>

</BODY>
</HTML>
My problem is in the function RandInt.
Can someone please help me?
Thanks

A1ien51
01-17-2004, 06:07 AM
I am guessing that the following is what you are looking for:

okay if you were to run a script like this:


<script>
for(i=0;i<30;i++){
document.write(Math.round(Math.random()*5));
}
</script>


you would see a result similiar to this:



212341102141114534155233532145


Now you need to know random returns a value from 0 to 1

Now the problem is that it is returning numbers from 0 to 5 since you are multiplying by 5

but you only have arrays from 0 to 4

but lets count
0 1 2 3 4 <-- Array Spot
1 2 3 4 5 <-- Count

0 1 2 3 4 5 <-- Random
1 2 3 4 5 6 <-- Count

so you do have 5 but the random number sees it as 6 so you need to subtract one from your total.

so

var Rnum=Math.round(Math.random()*4);

A1ien51
01-17-2004, 06:09 AM
Plus:

return Value[Rnum];

try

return Rnum;

A1ien51
01-17-2004, 06:14 AM
Plus

var RandValue=RandInt;
document.write(Quote);

to

var RandValue=RandInt();
document.write(MQuote(RandValue));


Plus

function RandInt(Rnum) {

to

function RandInt() {

coothead
01-17-2004, 07:53 AM
hi there student610,

I have taken the liberty of doing my
own script to solve your problem...
It is a little less involved...


<HTML>
<HEAD>
<TITLE>The Mark Twain Quotes Page</TITLE>
<script type="text/javascript">
//<![CDATA[
function MQuote()
{
var Quote=new Array();
Quote[0]='"I smoke in moderation, only one cigar at a time."';
Quote[1]='"Be careful of reading health books, you might die of a misprint."';
Quote[2]='"Man is the only animal that blushes--or needs to."';
Quote[3]='"Clothes make the man. Naked people have little or no influence on society."';
Quote[4]='"One of the most striking differences between a cat and a lie is that a cat has only nine lives."';

var i=Math.floor(Math.random()*Quote.length);
document.getElementById("random").innerHTML=Quote[i];
}
//]]>
</script>

</HEAD>

<BODY TEXT="#000000" LINKS="#00FF00" BACKGROUND="Paper.gif" onload="MQuote();">
<TABLE WIDTH=600 BORDER=1 CELLPADDING=5 CELLSPACING=0>

<!---Random Mark Twain Quote --->
<TR><TD id="random" BGCOLOR="#FFFFCC" COLSPAN=2 ALIGN=CENTER >
<FONT COLOR=BROWN FACE="Arial, Helvetica, Sans Serif" SIZE=2>
</FONT>
</TD></TR>



c:D:Dthead

Garadon
01-17-2004, 10:51 AM
My suggestion, here u just have to call the function like this
MQuote() and it will return a quote, and if u add more quotes u don't have to change anything.


<SCRIPT LANGUAGE="JavaScript">
<!--- Hide from older browsers
function MQuote(){
var Quote=new Array();
Quote[0]='"I smoke in moderation, only one cigar at a time."';
Quote[1]='"Be careful of reading health books, you might die of a misprint."';
Quote[2]='"Man is the only animal that blushes--or needs to."';
Quote[3]='"Clothes make the man. Naked people have little or no influence on society."';
Quote[4]='"One of the most striking differences between a cat and a lie is that a cat has only nine lives."';

var Rnum=Math.floor(Math.random()*Quote.length);
return Quote[Rnum];
}
//Stop hiding -->
</SCRIPT>

A1ien51
01-17-2004, 04:08 PM
Didn't you listen to my second post??? You need Quote.length-1!!

Garadon
01-17-2004, 05:37 PM
I did read ur post but unlike u I didn't use round I used floor which means that I don't have to deduct 1.

A1ien51
01-18-2004, 03:16 AM
~

student610
01-18-2004, 03:23 AM
Thanks A1ien51....you have been very helpful. I understand the problem better, but its still a bit foreign to me. I will keep working on solving this to get better customed to this logic.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum