473,394 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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="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
3 1850
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: The Lone Wolf | last post by:
hi, i'm new to php mysql and already have a problem with a simple routine $result=mysql_db_query(jcorp,"select count(*) as bar from users"); $count = mysql_result($result,0,"bar"); //<<<<<...
4
by: Keith Griffiths | last post by:
I'm trying to do a search under a set criteria followed by a selection of random entries meeting this criteria. But I don't seem to be able to achieve this. The idea being to search on say...
4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
5
by: newbie | last post by:
Hello, I face a practical problem with encryption. I've read examples for encrypting a file with the DES algorythm. The algorythm uses a key and a IV value. Both are 8 bytes if I'm correct,...
6
by: Steve Peterson | last post by:
Hi Probably a dumb question, but I need to generate a random number between 0 and 9. I was hoping that someone could help out with a code snipplet on how to do this. TIA Steve Peterson
5
by: Hareth | last post by:
Is this the correct way to generate random number? Dim myRandomObject As Random myRandomObject = New Random myRandomObject.Next(1, 4)
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
3
by: Laphan | last post by:
Hi All I use a MySQL DB with my ASP classic web app. I've been asked if I can create a routine whereby I get a random number of products (records) from the DB and display these on the site. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.