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 2275 kadghar 1,295
Recognized Expert Top Contributor
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
kadghar 1,295
Recognized Expert Top Contributor
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
kadghar 1,295
Recognized Expert Top Contributor
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
kadghar 1,295
Recognized Expert Top Contributor
... 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
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 of them being
the correct answer to the question, and the others not..
Here is my code.. (I already have the correct answer at array position
)
for($i=1;$i<=4;$i++)
{
|
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 url(../images/homepagebg3.jpg) no-repeat;
font:normal 90% Arial, Helvetica, sans-serif; color:#263158;}
|
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 to apply it
to these names.
Can anyone point out a sample I could use?
TIA!
Scott
|
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 selection
choices are listed top down in the order they appear in the XML file.
Here is an example of the XML formatting:
<question type="single_answer">The text of a question.
<answer correct="no">A. Answer One
<user_feedback>Incorrect...
| |
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 statement and
stored into a variable called headcount.
After that, I am using the following to initialize randomly generated
|
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: 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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |