Wrox Programmer Forums
|
ASP Pro Code Clinic As of Oct 5, 2005, this forum is now locked. No posts have been deleted. Please use "Classic ASP Professional" at: http://p2p.wrox.com/forum.asp?FORUM_ID=56 for discussions similar to the old ASP Pro Code Clinic or one of the other many remaining ASP and ASP.NET forums here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP Pro Code Clinic section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 9th, 2003, 02:17 PM
Registered User
 
Join Date: Jun 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default asp 2 dimensional array killing me

I've been playing with this 2 dimensional array for weeks and i think i've finally run out of ideas on what more to try. Basically it's a 2 dimensional array that i want to randomize by both rows and by columns. So looking either horizontally or vertically, i should not see any repeat numbers from 1-10. It works, but only in one way, either vert. or horiz. but not both ways (you can comment out the colgood = false or rowgood = false to see one of the pieces of code working, so currently colgood is disabled and so rows wise all the numbers should be unique) Could it be taking too long to process (script always times out) or is my code faulty ? thanks for looking and attempting, below is my code and below that is couple examples:


<%

dim random_number(9,9) '(10 columns,10 rows)
dim Rows, Columns, checkRows, checkColumns, colgood, rowgood

randomize

For Rows = 0 to 9
For Columns = 0 to 9

colgood = "true"
rowgood = "true"

random_number(Columns,Rows) = Int(Rnd * 10) + 1

for checkColumns= 0 to Columns - 1
if random_number(checkColumns, Rows)= random_number(Columns, Rows) then
'colgood = "false"
end if
next

if colgood = "true" then
for checkRows= 0 to Rows - 1
if random_number(Columns,checkRows) = random_number(Columns,Rows) then
rowgood = "false"
end if
next
end if

if (colgood = "false" OR rowgood = "false") then
Columns=Columns-1
end if
next
next

For Rows = 0 to 9
For Columns = 0 to 9
response.write random_number(Columns,Rows) & " "
next
response.write("<br><br>")
next
%>





here is the output when you comment out "colgood = false" (pick any column and you'll see random unique numbers from 1-10)

1 6 1 10 9 3 2 6 1 1

6 10 9 6 5 2 4 7 4 2

2 4 2 7 8 6 6 5 3 4

9 8 6 8 2 9 7 2 7 10

4 5 7 5 3 7 3 8 5 5

7 3 4 1 6 8 5 10 8 9

8 1 8 9 10 5 1 9 6 6

5 2 5 4 1 1 10 3 10 8

3 9 3 3 4 4 9 1 9 7

10 7 10 2 7 10 8 4 2 3

Comment: see how looking horizontally they repeat ?


now here is the code when you disable the "rowgood=false" line:
8 2 10 7 5 3 1 6 4 9

2 10 7 5 1 6 4 9 3 8

10 3 2 4 8 1 6 5 9 7

9 10 4 8 2 7 3 6 5 1

3 9 7 8 10 6 4 1 2 5

5 8 7 3 2 1 9 4 10 6

3 7 6 5 9 8 10 2 4 1

8 5 7 1 2 9 10 6 3 4

1 4 5 8 7 10 3 6 9 2

5 3 6 10 9 7 1 4 8 2

Comment: now if you look horizontally, they're unique, but vertically they are repeating

**i want it unique in both ways, no matter which row or column you choose**

this is just my version of the algorithm, if anyone knows of a pre-existing one, please please let me know

thanks much,
-PS
 
Old June 9th, 2003, 03:34 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I think what your missing is that random numbers, aren't. And with such a small pool (1-10) you are bound to see repetition of the numbers.

Your own check is what's causing the lack or repetition, the unexpected result.


Hal Levy
Daddyshome, LLC
 
Old June 9th, 2003, 08:03 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 215
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You could also store an array of values that have been used and, if one of those values is returned as random, get another value.
Note: This is actually *decreasing* randomness. A random number means that there is equal chance of any number being returned, and this includes numbers that have been previously been returned.

regards
David Cameron
 
Old June 10th, 2003, 10:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The problem is that you can get to situations that are impossible to resolve. This is because you are generating random numbers as you go along without looking at the larger picture. Here is an example, if I use a 3 x 3 array just to get the idea across, your routine has completed the first row and has done the first and second numbers of the second row. The array currently looks like this (for example):

Code:
1 2 3
2 1
The problem is that we can not use the number 3 because it would not make the third column unique, however this is the only number we can use, otherwise the column will not be unique. Therefore this particular array will never be able to be completed. This proves that if your application were able to complete the array, there would be a definite pattern to it. This may not be a problem for you, however remember that it can never be completely random.

One way round this would be to use 2 array's, one with all the number predefined. This predefined array would look something like the one below:

Code:
1  2  3  4  5  6  7  8  9  10
2  3  4  5  6  7  8  9  10 1
3  4  5  6  7  8  9  10 1  2
4  5  6  7  8  9  10 1  2  3
5  6  7  8  9  10 1  2  3  4
6  7  8  9  10 1  2  3  4  5
7  8  9  10 1  2  3  4  5  6
8  9  10 1  2  3  4  5  6  7
9  10 1  2  3  4  5  6  7  8
10 1  2  3  4  5  6  7  8  9
This array works just as your application requires, however it will be the same every time and there is no element of randomness to it. To get round this you could randomly generate 2 numbers, essentially an x and a y co-ordinate. These co-ordinates would be used to build a new series in your second array, therefore if you chose the x and y co-ordinates of 1 and 1 respectively, your routine would start from column 1, row 1 and your second array would be identical to the first. However if you chose column 3, row 6 then the new array would start from 8:

Code:
`       v

  1  2  3  4  5  6  7  8  9  10
  2  3  4  5  6  7  8  9  10 1
  3  4  5  6  7  8  9  10 1  2
  4  5  6  7  8  9  10 1  2  3
  5  6  7  8  9  10 1  2  3  4
> 6  7 [8] 9  10 1  2  3  4  5
  7  8  9 10 1  2  3  4  5  6
  8  9  10 1  2  3  4  5  6  7
  9  10 1  2  3  4  5  6  7  8
  10 1  2  3  4  5  6  7  8  9
  It would then use 9, 10, 1, 2, 3, 4 and 5 before starting from the beginning for the row again and use 6 and 7. The next row (row 2 in your new array) would start from column 3, row 7 of the predefined array (the column stays the same however the row increases by one). When the row has got to the last row (row 10) it would start from the first row and continue like this until it got to the starting row minus 1. Your second array would end up looking like this:

Code:
8  9  10 1  2  3  4  5  6  7
9  10 1  2  3  4  5  6  7  8
10 1  2  3  4  5  6  7  8  9
1  2  3  4  5  6  7  8  9  10
2  3  4  5  6  7  8  9  10 1
3  4  5  6  7  8  9  10 1  2
4  5  6  7  8  9  10 1  2  3
5  6  7  8  9  10 1  2  3  4
6  7  8  9  10 1  2  3  4  5
7  8  9  10 1  2  3  4  5  6
This way there are 100 different possible results for the second array. Bear in mind that there will always have to be a certain pattern to the results. If I can find the time I will try and work on some example algorithms for you.

The software required `Windows 95 or better', so I installed Linux.
 
Old June 12th, 2003, 08:47 PM
Registered User
 
Join Date: Jun 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you all very much for helping me, especially the time owain spent writing up all the numbers, hopefully I will be able to adapt to one of these suggestions and this algorithm will be solved.


-Pshah
 
Old June 17th, 2003, 09:06 PM
Registered User
 
Join Date: Jun 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I finally solved this magic square thing. It's foundation is based from owain's method

I randomly generate a first row of values from 1-10

Then I populate the next 9 rows based on the first row, just offsetting each row left or right by one (doesn't matter), just to make it unique both rows wise and columns wise.

Now the trick to make it look random: notice that you can swap any two rows or any two columns and still keep the idea intact with the new swapped values. So i run a loop 100 times to randomly just select two rows/two columns and start swapping two of them at a time.

Before you know it, you have a random 10 by 10 matrix where each row or column has unique values from 1-10
Thanks
-pshah





Similar Threads
Thread Thread Starter Forum Replies Last Post
using 2 dimensional array Abraham EJB 0 July 17th, 2007 09:27 AM
two-dimensional array Lizane Java Basics 4 May 23rd, 2007 09:35 AM
2 dimensional array trangd Beginning PHP 0 August 18th, 2005 12:37 AM
2 dimensional array venterjo General .NET 2 January 23rd, 2005 09:04 AM
3 dimensional array with MSChart Bogus Beginning VB 6 1 July 11th, 2003 03:47 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.