Results 1 to 10 of 10

Thread: For Loop with Random Never Stops!

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    For Loop with Random Never Stops!

    I am using the following code to create a dummy file of a specific size.
    RemSpace declared as a Long value and is returned in bytes from a separate function in my applciation. I have verified that it returns the correct # in bytes. However, when I execute the following routine, the dummyfile is created, but it never stops the loop. I have to kill it from task manager or it would fill up the HDD. What am I missing here? The line:

    Code:
    For i As Integer = 1 To RemSpace
    should tell the For loop to stop when it reaches the value of RemSpace, which is a long value in bytes. Here is my complete code.
    Thanks for any help with this one.


    Code:
    Public Sub Create_DummyFile()
    
            Dim RandomClass As New Random()
            Dim RandomNumber As Integer
            RandomNumber = RandomClass.Next()
    
            'Create the dummyfile to take up the remaining space on the drive
            Dim myStreamWriter As New IO.StreamWriter("C:\dummy.txt")
    
            For i As Integer = 1 To RemSpace
                myStreamWriter.Write(RandomNumber)
            Next
            myStreamWriter.Close()
            myStreamWriter.Dispose()
            myStreamWriter = Nothing
        End Sub

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: For Loop with Random Never Stops!

    The line For i As Integer = 1 To RemSpace is requesting that i which is an integer, is incremented to RemSpace which is a Long. The maximum value for an integer is -2,147,483,648 to +2,147,483,647.

    Is RemSpace bigger than that?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: For Loop with Random Never Stops!

    hmm.....

    RemSpace = 123971584

    Declaring i as a long value produces the same results. I guess I am misunderstanding here. I guess RemSpace should be declared as an integer as well?

  4. #4
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: For Loop with Random Never Stops!

    ok, here's what Id do, put a Label on your form (I'll call it Label1) and add this code in bold;

    Code:
    Public Sub Create_DummyFile()
    
            Dim RandomClass As New Random()
            Dim RandomNumber As Integer
            RandomNumber = RandomClass.Next()
    
            'Create the dummyfile to take up the remaining space on the drive
            Dim myStreamWriter As New IO.StreamWriter("C:\dummy.txt")
    
            For i As Integer = 1 To RemSpace
                myStreamWriter.Write(RandomNumber)
                Label1.Text = i.ToString & " (123971584)"
                Label1.Refresh()
            Next
            myStreamWriter.Close()
            myStreamWriter.Dispose()
            myStreamWriter = Nothing
        End Sub
    Now run the program and watch the value in the label, does it exceed RemSpace?

    Writing 123971584 numbers to a file, might take a while, so is it the case that its just taking longer than you expect?, rather than being stuck in a never ending loop?
    Last edited by Bulldog; Mar 1st, 2009 at 06:16 PM.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: For Loop with Random Never Stops!

    When I do that the application just runs extremely slow and the label counter goes up very slowly. If I take those lines out, I'm back to getting files a gig or larger. RemSpace is now declared as an integer, just as i is. It shouldn't be taking that long to create the file with the label displaying the numbers, should it?

  6. #6
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: For Loop with Random Never Stops!

    The intention is to write RemSpace bytes to the file? You're actually writing RemSpace * (Bytes in your RandomNumber)

    So to write "bytes" your random number needs to be 1 byte, so use;
    RandomNumber = RandomClass.Next(0, 10).

    But since this is not in your loop, you might was well write "0", since your not filling the file with random numbers, your just writing the same random number over and over again.

    So if you just write "0", what happens?

    Code:
            For i As Integer = 1 To RemSpace
                myStreamWriter.Write("0")
            Next
    You should end up with a file which is 118MB
    Last edited by Bulldog; Mar 1st, 2009 at 06:43 PM.

  7. #7
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: For Loop with Random Never Stops!

    Quote Originally Posted by Bulldog
    You should end up with a file which is 118MB
    Wouldn't that be 236MB? You are writing the Ascii character "0", which is two bytes.

    If he is writing an integer out (8 bytes), then the file would be 1GB in size.

  8. #8
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: For Loop with Random Never Stops!

    Yes, 236MB

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: For Loop with Random Never Stops!

    Two bytes because it is unicode. ASCII would be one byte.
    My usual boring signature: Nothing

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: For Loop with Random Never Stops!

    Code:
        Dim RandomClass As New Random()
        Public Sub Create_DummyFile(ByVal remSpace As Integer)
            Dim RandomNumber As Byte
            'Create the dummyfile to take up the remaining space on the drive
            Dim myStreamWriter As New IO.StreamWriter("C:\dummy.txt")
            For i As Integer = 1 To remSpace
                RandomNumber = CByte(RandomClass.Next(0, 256))
                myStreamWriter.BaseStream.WriteByte(RandomNumber)
            Next
            myStreamWriter.Close()
            myStreamWriter.Dispose()
            myStreamWriter = Nothing
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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