JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 17th, 2004, 03:55 PM
wendallsan wendallsan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 4 wendallsan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
building an array of random numbers

hi all,

I'm trying to figure out how to build an array of random numbers, supplying a max number to allow for the random number, and the number of items in the array. I've tried several approaches and have gotten varying results, none of them exactly what I want, so I figure it's time to ask someone who really knows how to do it. The code below currently creates an infinite loop in the section that calls the testNum function (I think), which is obviously not what I'm trying to produce. Any insight or help would be great, thanks! I am running this on the server side using an IIS 6.0 server (hence the use of the Response object, etc).

Here is my current code:

function buildArray(numOfItems, maxNum){
// numOfItems = number of items to put into the array
// maxNum = the max num of the random number
// theArray holds the numbers
theArray = new Array();
// y is used to hold the place of the current array item
y = 0;
// while y is less than numOfItems, keep trying to add items to the array
do {
// get a new random number
temp = getRand(maxNum);
// go thru array and test each array item against temp
for (x=0; x < theArray.length; x++){
// test
if (!testNum(temp, theArray)){
theArray.push(temp);
y++;
}
}
} while (y <= numOfItems);
return theArray;
}

function getRand(maxNum){
// generate random number between 0 and maxNum
randomNum = (Math.ceil(Math.random()*100))%maxNum;
return randomNum;
}

function testNum(testNum, theArray){
// testResult = whether testNum is found in theArray, init to 0;
testResult = 0;
for (i=0; i <= theArray.length; i++){
if (testNum == theArray[i]){
testResult = 1;
}
}
return testResult;
}

testArray = buildArray(3, 5);

Response.Write(testArray);

Reply With Quote
  #2  
Old February 19th, 2004, 08:51 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Are you getting an error with your current code?

If you're using javascript, why are you using Response.Write?
Is that not ASP.NET?

Javascript would be document.write, and I'm pretty sure you need to loop through the array to output it, simply outputting the variable won't work.

Reply With Quote
  #3  
Old February 19th, 2004, 01:08 PM
wendallsan wendallsan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 4 wendallsan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Actually, you can output the contents of an array by just using its name w/o a loop:

<script language="JavaScript">
theArray = [1, 2, 3, 4];
function putArray(whichArray){
document.write(whichArray);
}
putArray(theArray);
</script>

Run that to see for yourself . . .

You are correct about the Response object. I am running this javascript on the server side of an IIS 6.0 server. I just prefer to use javascript for this (er, "jscript") instead of VB-- VB is just too wordy for me. You could replace the Response.write commands with Document.write commands and run this on the client side just fine. The problem I'm running into is the testing an array as we create it bit. The process (will hopefully) works like this: a new random number is created, then that number is tested against all the items in the array to see if it is equal to any of them. If the number matches a number already in the array, a new random number is generated and the test is run again. If the number is unique (no matches in the array), then it is added to the array. This is where I'm running into problems, and again, this would run on the client side or server side fine.

Any suggestions?

Reply With Quote
  #4  
Old February 19th, 2004, 03:29 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
I don't exactly understand the error...
What's the outcome of this script?

The logic and syntax seems fine to me...
What happens when you run this?

Reply With Quote
  #5  
Old February 20th, 2004, 03:24 PM
wendallsan wendallsan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 4 wendallsan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
currently the script runs an infinite loop until the browser or server (depending on where you run it) times out.

you can try it yourself with the code below (this will run on the client side, no server needed):

<html>
<body>
<script language="JavaScript">
function buildArray(numOfItems, maxNum){
theArray = new Array();
y = 0;
do {
temp = getRand(maxNum);
for (x=0; x < theArray.length; x++){
if (!testNum(temp, theArray)){
theArray.push(temp);
y++;
}
}
} while (y <= numOfItems);
return theArray;
}
function getRand(maxNum){
return (Math.ceil(Math.random()*100))%maxNum;
}
function testNum(testNum, theArray){
testResult = 0;
for (i=0; i <= theArray.length; i++){
if (testNum == theArray[i]){
testResult = 1;
}
}
return testResult;
}
Response.Write(buildArray(3, 5));
</script>
</body>
</html>

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > building an array of random numbers


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT