Going nuts with random number routine HELP!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Davidson

    #1

    Going nuts with random number routine HELP!

    Here's what I'm trying to do:

    I have a string with 30 numbers in it. I want to generate a random
    number between 0 an 30, go to that position in the string and get that
    number. Here is the code you can run it and see it doesn't work. I'm
    new at this (only about a week) but I thought this should work. Please
    help.

    It's probably something simple, but I don't see it.

    <html>
    <SCRIPT LANGUAGE="JavaS cript1.3">

    var nRandom = 0;
    var nIndex = 0;
    var string_of_Numbe rs = "84902918462800 332874910329490 1"
    /* *************** *************** ***** */
    function buttonCheck()
    /* *************** *************** ***** */
    {
    if (document.carFo rm.spinButton.v alue == "Start")
    {
    document.carFor m.spinButton.va lue = "Stop";
    }
    else
    {
    document.carFor m.spinButton.va lue ="Start";

    nRandom = Math.round(Math .random() * 30 + 1);
    nIndex = string_of_Numbe rs.indexOf(nRan dom);

    document.carFor m.slot1.value = "Random number generated "+nRandom;
    document.carFor m.slot2.value = string_of_Numbe rs;
    document.carFor m.slot3.value = "Value located at that position
    "+nIndex;

    }
    }

    </SCRIPT>
    <body>

    <FORM NAME="carForm">
    <p>
    <INPUT TYPE=button VALUE=Start NAME="spinButto n"
    onClick=buttonC heck()>
    <p>
    <INPUT TYPE=text name="slot1" value="" size=50>
    <p>
    <INPUT TYPE=text name="slot2" value="" size=40>
    <p>
    <INPUT TYPE=text name="slot3" value="" size=50>
    </form>
    </body>
    </html>
  • Lee

    #2
    Re: Going nuts with random number routine HELP!

    Jim Davidson said:[color=blue]
    >
    >Here's what I'm trying to do:
    >
    >I have a string with 30 numbers in it. I want to generate a random
    >number between 0 an 30, go to that position in the string and get that
    >number. Here is the code you can run it and see it doesn't work. I'm
    >new at this (only about a week) but I thought this should work. Please
    >help.
    >
    >It's probably something simple, but I don't see it.
    >
    ><html>
    ><SCRIPT LANGUAGE="JavaS cript1.3">[/color]


    Since you don't actually require version 1.3, that should be:
    <script type="text/javascript">

    [color=blue]
    >var nRandom = 0;
    >var nIndex = 0;
    >var string_of_Numbe rs = "84902918462800 332874910329490 1"
    >/* *************** *************** ***** */
    >function buttonCheck()
    >/* *************** *************** ***** */
    >{
    > if (document.carFo rm.spinButton.v alue == "Start")
    > {
    > document.carFor m.spinButton.va lue = "Stop";
    > }
    > else
    > {
    > document.carFor m.spinButton.va lue ="Start";
    > nRandom = Math.round(Math .random() * 30 + 1);[/color]

    That line will generate numbers ranging from 1 to 31, with
    the first and last selected only half as often as any of
    the others. Since the character positions are numbered from
    0 to 29, you're going to have problems. To get values evenly
    distributed over the character positions in your string, use:

    nRandom = Math.floor(Math .random()*strin g_of_Numbers.le ngth);
    [color=blue]
    > nIndex = string_of_Numbe rs.indexOf(nRan dom);[/color]

    The indexOf() method doesn't do what you think it does.
    Change that line to:

    nIndex = string_of_Numbe rs.charAt(nRand om);
    [color=blue]
    > document.carFor m.slot1.value = "Random number generated "+nRandom;
    > document.carFor m.slot2.value = string_of_Numbe rs;
    > document.carFor m.slot3.value = "Value located at that position
    >"+nIndex;
    >
    > }
    >}
    >
    ></SCRIPT>
    ><body>
    >
    ><FORM NAME="carForm">
    ><p>
    > <INPUT TYPE=button VALUE=Start NAME="spinButto n"
    >onClick=button Check()>
    ><p>
    > <INPUT TYPE=text name="slot1" value="" size=50>
    ><p>
    > <INPUT TYPE=text name="slot2" value="" size=40>
    ><p>
    > <INPUT TYPE=text name="slot3" value="" size=50>
    ></form>
    ></body>
    ></html>[/color]

    Comment

    • Jim Davidson

      #3
      Re: Going nuts with random number routine HELP!

      Lee, what can I say...thank you, thank you, thank you! You don't know
      how long I sat here scratching my head wondering why it wouldn't work

      Thanks again

      Lee <REM0VElbspamtr ap@cox.net> wrote in message news:<bkjbn0026 22@drn.newsguy. com>...[color=blue]
      > Jim Davidson said:[color=green]
      > >
      > >Here's what I'm trying to do:
      > >
      > >I have a string with 30 numbers in it. I want to generate a random
      > >number between 0 an 30, go to that position in the string and get that
      > >number. Here is the code you can run it and see it doesn't work. I'm
      > >new at this (only about a week) but I thought this should work. Please
      > >help.
      > >
      > >It's probably something simple, but I don't see it.
      > >
      > ><html>
      > ><SCRIPT LANGUAGE="JavaS cript1.3">[/color]
      >
      >
      > Since you don't actually require version 1.3, that should be:
      > <script type="text/javascript">
      >
      >[color=green]
      > >var nRandom = 0;
      > >var nIndex = 0;
      > >var string_of_Numbe rs = "84902918462800 332874910329490 1"
      > >/* *************** *************** ***** */
      > >function buttonCheck()
      > >/* *************** *************** ***** */
      > >{
      > > if (document.carFo rm.spinButton.v alue == "Start")
      > > {
      > > document.carFor m.spinButton.va lue = "Stop";
      > > }
      > > else
      > > {
      > > document.carFor m.spinButton.va lue ="Start";
      > > nRandom = Math.round(Math .random() * 30 + 1);[/color]
      >
      > That line will generate numbers ranging from 1 to 31, with
      > the first and last selected only half as often as any of
      > the others. Since the character positions are numbered from
      > 0 to 29, you're going to have problems. To get values evenly
      > distributed over the character positions in your string, use:
      >
      > nRandom = Math.floor(Math .random()*strin g_of_Numbers.le ngth);
      >[color=green]
      > > nIndex = string_of_Numbe rs.indexOf(nRan dom);[/color]
      >
      > The indexOf() method doesn't do what you think it does.
      > Change that line to:
      >
      > nIndex = string_of_Numbe rs.charAt(nRand om);
      >[color=green]
      > > document.carFor m.slot1.value = "Random number generated "+nRandom;
      > > document.carFor m.slot2.value = string_of_Numbe rs;
      > > document.carFor m.slot3.value = "Value located at that position
      > >"+nIndex;
      > >
      > > }
      > >}
      > >
      > ></SCRIPT>
      > ><body>
      > >
      > ><FORM NAME="carForm">
      > ><p>
      > > <INPUT TYPE=button VALUE=Start NAME="spinButto n"
      > >onClick=button Check()>
      > ><p>
      > > <INPUT TYPE=text name="slot1" value="" size=50>
      > ><p>
      > > <INPUT TYPE=text name="slot2" value="" size=40>
      > ><p>
      > > <INPUT TYPE=text name="slot3" value="" size=50>
      > ></form>
      > ></body>
      > ></html>[/color][/color]

      Comment

      • Dr John Stockton

        #4
        Re: Going nuts with random number routine HELP!

        JRS: In article <eaa6451a.03092 10816.701ec6b5@ posting.google. com>, seen
        in news:comp.lang. javascript, Jim Davidson <raccoon@icubed .com> posted
        at Sun, 21 Sep 2003 09:16:10 :-
        [color=blue]
        >Lines: 81[/color]
        [color=blue]
        >Lee, what can I say...thank you, thank you, thank you! You don't know
        >how long I sat here scratching my head wondering why it wouldn't work
        >
        >Thanks again
        >
        >Lee <REM0VElbspamtr ap@cox.net> wrote in message news:<bkjbn0026 22@drn.newsguy. co
        >m>...[color=green]
        >> Jim Davidson said:[color=darkred]
        >> >
        >> >Here's what I'm trying to do:
        >> >
        >> >I h
        >> > ... ... ...[/color][/color][/color]

        Reading the FAQ on the subject would have given you the answer.
        It would also have guided you in the proper formatting of a Usenet News
        reply,

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