Hi,
I need a 2D array with 9 by 9 elements. The problem is that every column must be randomly filled with the numbers 1 To 9 (Or 0 To 8). But every row must contain exactly one 1, one 2 and one 3.
Can somebody please help me with building this array?
Cindy
13 2246
I need a 2D array with 9 by 9 ...
I see, something like a Sudoku, but without the 3x3 squares.
I dont know if there is an easy way, but with some DOs and FORs you can achieve it, something like this should work for the first column, you can use something like that for the next ones. - Dim i As Integer
-
Dim m As Integer
-
Dim IO As Boolean
-
Dim Arr(1 To 9, 1 To 9) As Integer
-
arr(1,1) = int (rnd * 9) + 1
-
i = 2
-
Do
-
IO = False
-
arr(1,i) = int (rnd*9) + 1
-
m = 1
-
Do
-
If arr(1,m) = arr(1,i) Then
-
IO = True
-
Exit Loop
-
End If
-
m = m + 1
-
If m = i Then Exit Do
-
Loop
-
If IO = False Then i = i + 1
-
If i = 10 Then Exit Do
-
Loop
I think there should be some method for sampling without replacement in wikipedia.
HTH
... with some DOs and FORs ...
I have written a code that produces a 9 by 9 array that contains randomly one "1" in each column and row: So there are never two or more "1" 's in a row or column. My code looks like this: - Dim j As Integer
-
Dim k As Integer = 8
-
Dim i As Integer
-
Dim x As Integer
-
-
Dim Arr(8, 8) As Integer
-
x = Int(Rnd() * 9)
-
Arr(x, 0) = 1
-
-
For i = 1 To 8
-
j = Int(Rnd() * 9)
-
k = 0
-
Do
-
Arr(j, i) = 1
-
If Arr(j, i) = Arr(j, k) Then
-
Arr(j, i) = 0
-
j = Int(Rnd() * 9)
-
k = 0
-
Else
-
k = k + 1
-
If k = i Then Exit Do
-
End If
-
Loop
-
Next
The next thing I want to do, is to add the numbers "2" in the same way and of course without overwriting the "1" 's. Can you please help me?
Cindy
The next thing I want to do, is to add the numbers "2" in the same way and of course without overwriting the "1" 's. Can you please help me?
Hi Cindy, I've made the code that adds all the numbers but there's a small problem here, the algorithm doesn't guarantee that you'll be making a valid arrangement. e.g. while assigning number "4" this might happen:
0 0 3 1 2 4 0 0 0
0 4 0 2 0 3 0 1 0
0 0 4 3 0 1 2 0 0
0 0 0 4 1 2 3 0 0
0 3 0 0 4 0 0 2 1
1 0 0 0 3 0 4 0 2
2 0 0 0 0 0 1 3 4
0 1 2 0 0 0 0 4 3
3 2 1 0 0 0 0 0 0
So you won't have a chance to write a 4 in the last line. I think the right way should be generating an ordered array like this:
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8
and then randomly switch rows and columns a random number of times.
HTH
and then randomly switch rows and columns a random number of times.
HTH
Thanks for your reply
I thought of that too, that there may be some inconsisticy while the array is building up.
In this case only the numbers 1 to 3 have to be at different rows and columns, so I won't encounter inconsistencies. But I want a general solution so that if I want the numbers 1 To 4 at different rows and columns, I only have to make little adjustments to the code.
I'm pretty new to vb2005, so I dont't know how to swap columns and rows until the requirements fit. Can you please help me?
Cindy
Hi Cindy, I've made the code that adds all the numbers but there's a small problem here, the algorithm doesn't guarantee that you'll be making a valid arrangement. e.g. while assigning number "4" this might happen:
Hi kadghar,
My code produces now a random 9 by 9 array: Both the numbers "1" and "2" are randomized. But even with two random numbers in a 9 by 9 array there will be eventualy an invalid arrangement. So you'r right, The best way is to swap rows and columns until the requierments fit. Can you help me with that please?
Cindy
Hi kadghar,
My code produces now a random 9 by 9 array: Both the numbers "1" and "2" are randomized. But even with two random numbers in a 9 by 9 array there will be eventualy an invalid arrangement. So you'r right, The best way is to swap rows and columns until the requierments fit. Can you help me with that please?
Cindy
Yeap, sorry i didnt answer before, i've been offline for 2 days and now i've a lot of work at the office.
to generate the initial array do something like - dim arr(8,8) as integer
-
dim i as integer
-
dim j as integer
-
-
for i=0 to 8
-
for j = 0 to 8
-
arr(i,j) = ((i + j) mod 9 ) +1
-
next
-
next
Depends on de version of vb you are using if you have the mod function. in case you dont, just create it for positive numbers, its quite easy: - public function mod(dou1 as double, dou2 as double) as double
-
mod = dou1 - int (dou1/dou2) * dou2
-
end function
and use mod(i+j , 9 ) + 1 instead.
And then i think you can use randomize to change the seed of the generator and make between 50 and 100 events, do something like: - dim n as integer
-
randomize
-
n=rnd*50 + 51
-
-
for i = 1 to n
-
arr = swap (arr, int(rnd*8), int(rnd*8), int(rnd * 2)+1)
-
next
i dont know if there's an easy way, but you can create a function like
swap ( swArr, Index1 , Index2, Dimension)
with a couple of FOR or DO and a dummy one-dimensioned array
And then i think you can use randomize to change the seed of the generator and make between 50 and 100 events, do something like: - dim n as integer
-
randomize
-
n=rnd*50 + 51
-
-
for i = 1 to n
-
arr = swap (arr, int(rnd*8), int(rnd*8), int(rnd * 2)+1)
-
next
i dont know if there's an easy way, but you can create a function like
swap ( swArr, Index1 , Index2, Dimension)
with a couple of FOR or DO and a dummy one-dimensioned array
Thanks for your reply,
I'm sorry I'm still asking you about the same issue I 'm troubled with. The problem is not to make an ordered array. And even randomize the array is not the real problem. The biggest challenge for me is to make the requirements fit, and when they fit to stop the procedure. I haven't a clue how to mennage that. I also don't understand what you mean with n-times events. Because you still don't know if the requierments will fit in that amount of events...do you? Please, can you help me again? ...Almost forgot: What do you mean with a dummy one-dimensional array
Cindy
... The biggest challenge for me is to make the requirements fit, and when they fit to stop the procedure. I haven't a clue how to mennage that. I also don't understand what you mean with n-times events...
What do you mean with requirements?? If you want the array to have always numbers 1 to 9 without repeating any in columns and rows, all you have to do is with the original array i gave you... the 123456789 , 23456781 ... and swap its rows and cols, it will always preserve this property... nice isnt it?
n-times mean that you can swap columns or lines a random number of times, lets say n times, where n will be a random number (lets say betwen 50 and 100), so
n= int (rnd * 51 + 50)
but you can do it 30 times or the number you like.
Almost forgot: What do you mean with a dummy one-dimensional array
Cindy
I mean an array that you will dispose after you used it while swaping columns
lets say you want to swap column 2 and 4
what you should do is copy column 2 into this "dummy array" then copy column 4 into column 2 and then "dummy array" into column 4
What do you mean with requirements?? If you want the array to have always numbers 1 to 9 without repeating any in columns and rows, all you have to do is with the original array i gave you... the 123456789 , 23456781 ... and swap its rows and cols, it will always preserve this property... nice isnt it?
Thank you very much! My code produces now a random array by swapping rows and colums of an ordered array. It looks very nice, but as you probebly know, the array is not really random. Let's take a look at a much simpler ordered array:
1234
2341
3412
4123
Indeed, you can swap rows and colums and the requierments (:every row and column can only have each number just ones) will always fit. But with this initial ordered array you can never make the following array:
3142
4321
1234
2413
So the requierments do fit, but the array can never be made by swapping. My question is: Do you have suggestions to make a really random array? I succeeded in making an array with two random numbers in a x by x array. But it did requiere a lot of code to make just two numbers random.
Cindy
Thank you very much! My code produces now a random array by swapping rows and colums of an ordered array. It looks very nice, but as you probebly know, the array is not really random. Let's take a look at a much simpler ordered array:
1234
2341
3412
4123
Indeed, you can swap rows and colums and the requierments (:every row and column can only have each number just ones) will always fit. But with this initial ordered array you can never make the following array:
3142
4321
1234
2413
So the requierments do fit, but the array can never be made by swapping. My question is: Do you have suggestions to make a really random array? I succeeded in making an array with two random numbers in a x by x array. But it did requiere a lot of code to make just two numbers random.
Cindy
instead of thinking of
1234
2341
3412
4123
as a base array, think of it as an "index map"
so generate a random vector, something that could look like:
Arr = {3,2,1,4}
and use (imagine indexes go from 1 to 4)
arr(1)arr(2)arr(3)arr(4)
arr(2)arr(3)arr(4)arr(1)
arr(3)arr(4)arr(1)arr(2)
arr(4)arr(1)arr(2)arr(3)
so the original array will look like:
3214
2143
1432
4321
And you can swap its columns and rows.
This way you can increase the posibilities. even then it'll have some restrictions.
HTH
By the way, take a look to this thread. It has a similar problem, and the solution there has even more restrictions, if you can find a way to make some better arrays will be great.
3214
2143
1432
4321
And you can swap its columns and rows.
This way you can increase the posibilities. even then it'll have some restrictions.
HTH
By the way, take a look to this thread. It has a similar problem, and the solution there has even more restrictions, if you can find a way to make some better arrays will be great.
That's a good idea, using different index-arrays. I don't want to sound "hard to please", but I want a really random array. I made some examples (on paper), with different index-arrays. It's still impossible to make the array I have written above. So, do you have other suggestions for me?
BTW I looked at the thread of Sudoku.
Cindy
... I don't want to sound "hard to please", ...
oh no!! how can you even say it!!! :P just kidding
I dont know how many permutations this problem has, but in case you find it out, you'll have to have a "basic structure" for each one, so you could randomly chose one of this structures in the begining, use it as a map of indexes and make the swaps..
But at least we have now two basic structures, my 1234 etc... and your imposible one.
If you find another case unreachable by any of these two we already have, you can use it as a third one... and so on.
Sorry to drop into the thread so late, but have you tried reading through the Wikipedia entry on magic squares?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Joe Six-Pack |
last post by:
Hi,
Im having problems in randomly selecting an element in an array that
has not been selected before.. in other words, I have an array of
answers to questions, then I want to select 5, with one...
|
by: Nathan Given |
last post by:
Hello All,
I am trying to randomly change the background image of my home page
but I can't seem to figure it out.
Here is a snippet of my css
....
BODY {background:transparent...
|
by: James |
last post by:
Just learning C#.
What's the easiest way to assign numbers 1-10 randomly to an array?
Basically I just want to take the numbers 1-10 and arrange them randomly in
slots 0-9 of an array.
Thanks
|
by: Scott Gordo |
last post by:
<!--Newbie warning-->
I've got a csv file with names, addresses, emails, etc.
I've been asked to randomly select a name from a csv file.
I've found plenty of RandNum examples, but I'm not sure how...
|
by: TPK |
last post by:
I have an HTML document with Javascript where I have a portion of the
page contents, a series of questions, being pulled from a XML file.
When the page dynamically builds all the questions and...
|
by: mdh2972 |
last post by:
If I have a .js file filled with data like links. how do i access just
1 element and bring it on my page
|
by: vadapallyraju |
last post by:
Hi Friends,
I am relatively new to this generation of variables in ASP. I am unable
to proceed from here. Can somebody advise ?
Challenge :
I get number of participants in an exam, from a sql...
|
by: oll3i |
last post by:
how to eg from an array take randomly a word so i can set other var with that value
|
by: John |
last post by:
How can I fill an array randomly so it contains a certian range of numbers
(1 - 100 for example) ? My Goal is to generate a set of numbers in random
order.
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |