Going nuts with random number routine HELP!

J

Jim Davidson

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="JavaScript1.3">

var nRandom = 0;
var nIndex = 0;
var string_of_Numbers = "849029184628003328749103294901"
/* *********************************** */
function buttonCheck()
/* *********************************** */
{
if (document.carForm.spinButton.value == "Start")
{
document.carForm.spinButton.value = "Stop";
}
else
{
document.carForm.spinButton.value ="Start";

nRandom = Math.round(Math.random() * 30 + 1);
nIndex = string_of_Numbers.indexOf(nRandom);

document.carForm.slot1.value = "Random number generated "+nRandom;
document.carForm.slot2.value = string_of_Numbers;
document.carForm.slot3.value = "Value located at that position
"+nIndex;

}
}

</SCRIPT>
<body>

<FORM NAME="carForm">
<p>
<INPUT TYPE=button VALUE=Start NAME="spinButton"
onClick=buttonCheck()>
<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>
 
L

Lee

Jim Davidson said:
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="JavaScript1.3">


Since you don't actually require version 1.3, that should be:
var nRandom = 0;
var nIndex = 0;
var string_of_Numbers = "849029184628003328749103294901"
/* *********************************** */
function buttonCheck()
/* *********************************** */
{
if (document.carForm.spinButton.value == "Start")
{
document.carForm.spinButton.value = "Stop";
}
else
{
document.carForm.spinButton.value ="Start";
nRandom = Math.round(Math.random() * 30 + 1);

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()*string_of_Numbers.length);
nIndex = string_of_Numbers.indexOf(nRandom);

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

nIndex = string_of_Numbers.charAt(nRandom);
 
J

Jim Davidson

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
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
Lines: 81
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

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top