Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jan 2004
    Location
    minnesota
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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>
    <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
    Last edited by student610; 01-17-2004 at 02:27 AM.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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);
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Plus:

    return Value[Rnum];

    try

    return Rnum;
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Plus

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

    to

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


    Plus

    function RandInt(Rnum) {

    to

    function RandInt() {
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #5
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,680
    Thanks
    0
    Thanked 230 Times in 225 Posts
    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>
    <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>
    cthead
    Last edited by coothead; 01-17-2004 at 06:43 PM.

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #7
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Didn't you listen to my second post??? You need Quote.length-1!!
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    ~
    Last edited by A1ien51; 01-18-2004 at 03:19 AM.
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #10
    New Coder
    Join Date
    Jan 2004
    Location
    minnesota
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    •