Results 1 to 5 of 5

Thread: [2008] Randomness and arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    14

    Question [2008] Randomness and arrays

    Im making a random cipher generator and here's what it looks like:

    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

    This pattern is repeated for the lower case letters, numbers (0-9), and symbols (< > # ...). I all ready have this:

    Code:
    Public Class NcryptKeyGen
        Dim strStatic(92) As String
        Dim strDynamic(92) As String
        Dim Y As Integer = 93
    
        Public Function X() As Integer
            Randomize()
            X = Int(Rnd() * Y)
    
            MsgBox(Y)
        End Function
        Private Sub btnGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGO.Click
            MsgBox("Generating code...")
    
            lblGTp.Text = strStatic(X())
            Y = Y - 1
            lblLTp.Text = strStatic(X())
            Y = Y - 1
        End Sub
    The arrays are way too long to post...but they're there. The reason that Y=Y-1 is so that the maximum parameter goes down one each time the function occurs. What i need to do to, is ensure that this remains a function where one value is assigned to one value so that i do not end up with A = @ and A = %. To do this, i believe that i need to delete the value i used to set the text property of a label.

    So, when % is assigned to A (at random), % will no longer be chosen and deleted...which is why the max parameter of the number generator is moved down one each cycle. So, how would i delete the just used element from an array during run time and after the Public Function X() is executed? i would appreciate any help with this matter.
    Last edited by RILEY343; Mar 8th, 2009 at 12:08 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Randomness and arrays

    First up, don't use Randomize and Rnd to generate random numbers. Create a Random object and then call its Next method.

    As for the question, see this CodeBank thread of mine.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    14

    Re: [2008] Randomness and arrays

    I don know how to use the code you gave me. Im looking over it and i thing i can make it work but my main question is how to i change it around so that it chooses between 93 different characters?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Randomness and arrays

    You're making a common mistake of looking just at the code rather than considering what the code does in a general sense. That code takes all the potential values and adds them to a collection. It then removes each value from that collection as it's used, so it can't be used again. That's all you have to do.

    You're using Char values rather than Integers so you simply create a List(Of Char) and call AddRange to add the contents of the array you already have. Then, when you generate a random number you use it to index the collection instead of the array. You then call RemoveAt to remove that item from the collection so it cannot be used again. If you do generate the same random number again it will be the index of a different Char.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    14

    Re: [2008] Randomness and arrays

    How does this look: (I've probably made some silly mistake somewhere)

    Code:
       1.
          Dim list As New ArrayList
    
                  For i As String = 0 To 92
                      list.Add(i)
                  Next i
    
                  Dim rand As New Random
                  Dim index As Integer
                  Dim item As Object
    
                  While list.Count < 0                  
                      index = rand.Next(0, list.Count)
    
                      item = list(index)
     
                      list.RemoveAt(index)
     
                  End While
    In the mean time...im going to test it on a scrap program.

    Edit: Its wrong. played around with it for a while....wouldnt stop returning the first index's value...0. I changed 0 to zero to make sure it was the first index and not some totally weird mistake.
    Last edited by RILEY343; Mar 9th, 2009 at 08:52 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width