Unique ramdomized numbers
-
i got the numbers 0 to 9 to place in rows and columns for a Sudoku Game, i have managed to code all i need exept for the unique numbers code. All the code is done in VB 6 becuse that is what my crappy school and budget can manage for licenses... >_< the code for the Sudoku Game (form2 is only a credits window)
Option Explicit Private Sub Cmd_cred_Click() Form2.Visible = True End Sub Private Sub cmd_exit_Click() End End Sub Private Sub cmd_gen_Click() Dim a As Integer For a = 0 To 80 Text1(a) = Int(Rnd * 2) If Text1(a) = 0 Then Text1(a) = "" End If Next Dim c As Integer For c = 0 To 80 If Text1(c).Text = "1" Then Text1(c) = Int(Rnd * 10) End If Next Dim d As Integer For d = 0 To 80 If Text1(d).Text = "0" Then Text1(d) = "" End If Next End Sub Private Sub cmd_reset_Click() Dim b As Integer For b = 0 To 80 Text1(b) = "" Next Form1.BackColor = &HE0E0E0 End Sub Private Sub Form_Load() Form1.BackColor = &HE0E0E0 End Sub
I have been using textboxes wit a control array for this progran i hope you guys can help me thanks in advance :) -
i got the numbers 0 to 9 to place in rows and columns for a Sudoku Game, i have managed to code all i need exept for the unique numbers code. All the code is done in VB 6 becuse that is what my crappy school and budget can manage for licenses... >_< the code for the Sudoku Game (form2 is only a credits window)
Option Explicit Private Sub Cmd_cred_Click() Form2.Visible = True End Sub Private Sub cmd_exit_Click() End End Sub Private Sub cmd_gen_Click() Dim a As Integer For a = 0 To 80 Text1(a) = Int(Rnd * 2) If Text1(a) = 0 Then Text1(a) = "" End If Next Dim c As Integer For c = 0 To 80 If Text1(c).Text = "1" Then Text1(c) = Int(Rnd * 10) End If Next Dim d As Integer For d = 0 To 80 If Text1(d).Text = "0" Then Text1(d) = "" End If Next End Sub Private Sub cmd_reset_Click() Dim b As Integer For b = 0 To 80 Text1(b) = "" Next Form1.BackColor = &HE0E0E0 End Sub Private Sub Form_Load() Form1.BackColor = &HE0E0E0 End Sub
I have been using textboxes wit a control array for this progran i hope you guys can help me thanks in advance :)When selecting a number for a square, there are three ways that you need to check for duplicates: In the same horizontal line, in the same vertical line, and in the same 3x3 box. Still, if you just pick numbers on random you will most of the time end up in a situation where a squre has no possible values at all. Unless you handle this and make the code start over, you will get stuck in an eternal loop. Creating a correct sudoku puzzle is a bit more complicated than just placing some random numbers. To be correct the puzzle has to have exactly one possible solution, no more, no less. Perhaps you can find some useful information here: www.setbb.com/sudoku[^]. --- b { font-weight: normal; }
-
When selecting a number for a square, there are three ways that you need to check for duplicates: In the same horizontal line, in the same vertical line, and in the same 3x3 box. Still, if you just pick numbers on random you will most of the time end up in a situation where a squre has no possible values at all. Unless you handle this and make the code start over, you will get stuck in an eternal loop. Creating a correct sudoku puzzle is a bit more complicated than just placing some random numbers. To be correct the puzzle has to have exactly one possible solution, no more, no less. Perhaps you can find some useful information here: www.setbb.com/sudoku[^]. --- b { font-weight: normal; }
see, that is why i need the unique numbers code... i can prolly toy around with the code and make it produce random unique numbers but i do need that code fisrt... btw, i followed your advice and put a post at setbb ;)
-
see, that is why i need the unique numbers code... i can prolly toy around with the code and make it produce random unique numbers but i do need that code fisrt... btw, i followed your advice and put a post at setbb ;)
Well, as I said you have to check horizontally, vertically and in the 3x3 box for duplicates. Start with all numbers 1-9 and loop through the related squares and remove the ones that are used. Then randomly pick one of those left. By the way, you can download Visual Studio Express versions for free from Microsoft, so it's not a big strain on the budget... --- b { font-weight: normal; }
-
Well, as I said you have to check horizontally, vertically and in the 3x3 box for duplicates. Start with all numbers 1-9 and loop through the related squares and remove the ones that are used. Then randomly pick one of those left. By the way, you can download Visual Studio Express versions for free from Microsoft, so it's not a big strain on the budget... --- b { font-weight: normal; }
would it not be simpler to put a code string before the randomization process that prohibits doubble numbers for each row, column and 3x3 box?
-
would it not be simpler to put a code string before the randomization process that prohibits doubble numbers for each row, column and 3x3 box?
A random number is just a floating point number between 0.0 and 1.0. There is no code that prohibits certain ranges for a random number. You have to take that whole range and distribute it equally over the possible numbers. If all numbers are used except 1, 4, 6 and 7, you distribute the random number range equally over those four numbers: 0.00 <= Rnd < 0.25 --> 1 0.25 <= Rnd < 0.50 --> 4 0.50 <= Rnd < 0.75 --> 6 0.75 <= Rnd < 1.00 --> 7 The easiest way of doing that is of course to pick a random number between 0 and 3, and translate to the available numbers. --- b { font-weight: normal; }