Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Unique ramdomized numbers

Unique ramdomized numbers

Scheduled Pinned Locked Moved Visual Basic
game-devdata-structureshelp
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Solid Snake
    wrote on last edited by
    #1

    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 :)

    G 1 Reply Last reply
    0
    • S Solid Snake

      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 :)

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      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; }

      S 1 Reply Last reply
      0
      • G Guffa

        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; }

        S Offline
        S Offline
        Solid Snake
        wrote on last edited by
        #3

        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 ;)

        G 1 Reply Last reply
        0
        • S Solid Snake

          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 ;)

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          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; }

          S 1 Reply Last reply
          0
          • G Guffa

            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; }

            S Offline
            S Offline
            Solid Snake
            wrote on last edited by
            #5

            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?

            G 1 Reply Last reply
            0
            • S Solid Snake

              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?

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              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; }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups