Page 1 of 1

Random length For Loop

Posted: Fri Jun 21, 2024 2:48 pm
by matalog
I'm trying to understand why this doesn't work:

Code: Select all

For x=1 To 501
  CreateFile(0,Str(x))
  For t=1 To 5+Random(50000)
    WriteByte(0,Random(255))
  Next
  CloseFile(0)
  Delay(100)
Next
I understand how to get it to work, but not why it doesn't work as it is. It produces files no bigger than 1kB.


The following code does what I wanted, but I don't know why the first didn't.

Code: Select all

For x=1 To 501
  CreateFile(0,Str(x))
  r.i=Random(50000)
  For t=1 To 5+r
    WriteByte(0,Random(255))
  Next
  CloseFile(0)
  Delay(100)
Next

Re: Random length For Loop

Posted: Fri Jun 21, 2024 2:58 pm
by Axolotl
Maybe I cannot explain it entirely.

Code: Select all

  For t=1 To 5+Random(50000)
The code always creates a new value for <expression2>.
If one value <expression1> is bigger than <expression2> the loop ends. This can perhaps always be with the same value.

In the second example the <expression2> is constant during the looping.