dcsimg
Results 1 to 4 of 4

Thread: Random number generator and linear search on text file

  1. #1

    Thread Starter
    Registered User
    Join Date
    Apr 2019
    Posts
    7

    Random number generator and linear search on text file

    I'm wondering how I'm able to save a random number generator to a text file I named "useraccountnumber" (on MS note) and also how to ensure the random number generatir creates the number and will not generate that same one later. I know it must somehow include a linear search through the "useraccountnumber" file, if that line does exist, I want it so that the user has to create a new random number.

    If you guys could help, that'd be appreciated.


    Code:
    Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
           
    
            Dim FILE_NAME As String = "C:\Users\Owner\Documents\test.txt"
            Dim rn As Random ' set rn as random
            Dim number As String ' sets number as string
            rn = New Random
            number = rn.Next(0, 999999) ' create integer or account number between 0 and 999999
            txtAccountNumber.Text = number ' put the random integer into the textbox
    
    
            ‘ I was told it would be a good idea to place the linear search on the file here
    
    
            If System.IO.File.Exists(FILE_NAME) = True Then
                Dim objWriter As IO.StreamWriter = IO.File.AppendText("useraccountnumber.txt")
                objWriter.WriteLine(txtAccountNumber.Text)
                objWriter.Close()
            End If
    
    
    End Sub
    Last edited by Shaggy Hiker; Jul 14th, 2019 at 10:11 AM. Reason: Added CODE tags.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,932

    Re: Random number generator and linear search on text file

    You can format code by pressing the # button and pasting the code between the resulting [CODE][/CODE] tags. Alternatively, you can paste in the code, then highlight it and press the # button. Some people prefer the VB button.

    I'm a bit unclear as to what has to be unique. It sounds like it isn't the file name, but you are keeping track of a series of numbers in the file, and those numbers need to be unique. Assuming that's right, the next question I would ask is: How big will this file get?

    The reason I ask is that searching the file, while possible, will probably be a bit slow. What would be ideal would be to bring all the data from the file into a list in memory and search that. However, if the file gets above some size, that could end up being a worse solution. The size where that becomes bad would be pretty large, so you might ignore that, but it's a thought.

    To pursue that approach, you might use ReadAllLines to bring every line from the file into an array of strings. Since these are integers, I'd be wanting to convert that array of strings into a List(of Integer), though it would actually work fairly well either way, so that step might be reasonable to skip. Searching for a unique in an array or list is pretty simple, as you could do this:

    dim n = getANewNumber
    Do While yourList.Contains(n)
    n = getANewNumber
    Loop

    yourList.Add(n)

    Those would be the rough steps. You could then save yourList to the file right away, or you could wait and save it at a later date. Saving right away is probably the best solution, but you might wait just a bit. Basically, you want to save at the point where a catastrophic power failure won't cost you any data. For example, if you save right away, but you wanted to associate some data with that number later on, then a power failure at that moment would mean that you'd have a number in the file with no data associated with it. Therefore, you might wait to save the data until you are ready to save the number and the data associated with it at the same time.
    My usual boring signature: Nothing

  3. #3
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    896

    Re: Random number generator and linear search on text file

    You don't need to create a string variable "number". You can add the .tostring method at the end of the integer variable.

    All you need is a stream reader to read one line at a time from the file. Click here for how to do that. I would create a "Found" Boolean variable before the loop. Default is False. If in the look it ever matches one of the lines, set Found to true and exit the loop. All this would be in a loop that would repeat while false. Odds are it will never loop more than once, but this way you will be certain.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,356

    Re: Random number generator and linear search on text file

    Code:
    number = rn.Next(0, 999999) ' create integer or account number between 0 and 999999
    that's incorrect... it creates an integer or account number between 0 and 999998

    to create an integer or account number between 0 and 999999, use...

    Code:
    number = rn.Next(0, 1000000) ' create integer or account number between 0 and 999999
    The upperbound is exclusive

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width