|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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); |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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? |
|
#4
|
||||
|
||||
|
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? |
|
#5
|
|||
|
|||
|
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> |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > building an array of random numbers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|