Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Math.random problem

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>
<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>

<!---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.
Thanks

• I am guessing that the following is what you are looking for:

okay if you were to run a script like this:
Code:
```<script>
for(i=0;i<30;i++){
document.write(Math.round(Math.random()*5));
}
</script>```
you would see a result similiar to this:

Code:
`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);

• Plus:

return Value[Rnum];

try

return Rnum;

• Plus

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

to

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

Plus

function RandInt(Rnum) {

to

function RandInt() {

• hi there student610,

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

Code:
```<HTML>
<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>

<!---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>```

• 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.
Code:
```
<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>```

• Didn't you listen to my second post??? You need Quote.length-1!!

• 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.

• ~

• 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.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•