By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

Going nuts with random number routine HELP!

P: n/a
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>
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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:
<script type="text/javascript">

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


Jul 20 '05 #2

P: n/a
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 <RE**************@cox.net> wrote in message news:<bk*********@drn.newsguy.com>...
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:
<script type="text/javascript">

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

Jul 20 '05 #3

P: n/a
JRS: In article <ea**************************@posting.google.com >, seen
in news:comp.lang.javascript, Jim Davidson <ra*****@icubed.com> posted
at Sun, 21 Sep 2003 09:16:10 :-
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

Lee <RE**************@cox.net> wrote in message news:<bk*********@drn.newsguy.co
m>...
Jim Davidson said:
>
>Here's what I'm trying to do:
>
>I h
> ... ... ...


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.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.