473,385 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Randomly filled Array

cindy2
35
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
Sep 25 '07 #1
13 2246
kadghar
1,295 Expert 1GB
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.
Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer
  2. Dim m As Integer
  3. Dim IO As Boolean
  4. Dim Arr(1 To 9, 1 To 9) As Integer
  5. arr(1,1) = int (rnd * 9) + 1
  6. i = 2
  7. Do
  8.     IO = False
  9.     arr(1,i) = int (rnd*9) + 1
  10.     m = 1
  11.     Do 
  12.         If arr(1,m) = arr(1,i) Then 
  13.             IO = True
  14.             Exit Loop
  15.         End If
  16.         m = m + 1
  17.         If m = i Then Exit Do
  18.     Loop
  19.     If IO = False Then i = i + 1 
  20.     If i = 10 Then Exit Do
  21. Loop
I think there should be some method for sampling without replacement in wikipedia.

HTH
Sep 25 '07 #2
cindy2
35
... 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:
Expand|Select|Wrap|Line Numbers
  1.     Dim j As Integer
  2.     Dim k As Integer = 8
  3.     Dim i As Integer
  4.     Dim x As Integer
  5.  
  6.     Dim Arr(8, 8) As Integer
  7.     x = Int(Rnd() * 9)
  8.     Arr(x, 0) = 1
  9.  
  10.     For i = 1 To 8
  11.         j = Int(Rnd() * 9)
  12.         k = 0
  13.         Do
  14.             Arr(j, i) = 1
  15.             If Arr(j, i) = Arr(j, k) Then
  16.                 Arr(j, i) = 0
  17.                 j = Int(Rnd() * 9)
  18.                 k = 0
  19.             Else
  20.                 k = k + 1
  21.                 If k = i Then Exit Do
  22.             End If
  23.         Loop
  24.     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
Sep 26 '07 #3
kadghar
1,295 Expert 1GB
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
Sep 27 '07 #4
cindy2
35
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
Sep 27 '07 #5
cindy2
35
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
Sep 28 '07 #6
kadghar
1,295 Expert 1GB
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

Expand|Select|Wrap|Line Numbers
  1. dim arr(8,8) as integer
  2. dim i as integer
  3. dim j as integer
  4.  
  5. for i=0 to 8
  6.     for j = 0 to 8
  7.         arr(i,j) = ((i + j) mod 9 ) +1
  8.     next
  9. 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:

Expand|Select|Wrap|Line Numbers
  1. public function mod(dou1 as double, dou2 as double) as double
  2.     mod = dou1 - int (dou1/dou2) * dou2
  3. 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:

Expand|Select|Wrap|Line Numbers
  1. dim n as integer
  2. randomize 
  3. n=rnd*50 + 51
  4.  
  5. for i = 1 to n
  6.     arr = swap (arr, int(rnd*8), int(rnd*8), int(rnd * 2)+1)
  7. 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
Oct 1 '07 #7
cindy2
35
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:

Expand|Select|Wrap|Line Numbers
  1. dim n as integer
  2. randomize 
  3. n=rnd*50 + 51
  4.  
  5. for i = 1 to n
  6.     arr = swap (arr, int(rnd*8), int(rnd*8), int(rnd * 2)+1)
  7. 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
Oct 4 '07 #8
kadghar
1,295 Expert 1GB
... 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
Oct 4 '07 #9
cindy2
35
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
Oct 9 '07 #10
kadghar
1,295 Expert 1GB
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.
Oct 9 '07 #11
cindy2
35
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
Oct 9 '07 #12
kadghar
1,295 Expert 1GB
... 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.
Oct 9 '07 #13
Killer42
8,435 Expert 8TB
Sorry to drop into the thread so late, but have you tried reading through the Wikipedia entry on magic squares?
Oct 10 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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...
4
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...
4
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
2
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...
2
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...
2
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
3
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...
3
oll3i
by: oll3i | last post by:
how to eg from an array take randomly a word so i can set other var with that value
5
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.
1
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...
0
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...
0
isladogs
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...
0
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...
0
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$) { } ...
0
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...
0
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
0
BarryA
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...
1
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.