✓ Accepted answer
I'm having trouble understanding your question, but I'll attempt to answer what I presume you mean to ask.
The for statement evaluates its provided expressions once, before the loop starts. With this in mind, we can provide a call to a random number generator as the numeric for's second expression to have it iterate until that number:
local random = Random.new()
for i = 1, random:NextInteger(1, 100) do
print(i)
end
Note my use of the Random class, which has a more random algorithm compared to math.random and is recommended to be used.
You can learn more about the numeric for statement here.
##Edit
I noticed that you provided an explanation of your question in the comments, so I'll describe how this can be achieved.
As I've previously said, all of the for statement's expressions are evaluated once, prior to the loop starting. For this reason, we cannot change the third increment expression every iteration—we'll have to create our own loop that's similar to the numeric for to provide this functionality:
local random = Random.new()
do
local start_n = 1
local end_n = 100
local min, max = 1, 20
local i = start_n
while i <= end_n do
i = i + random:NextInteger(min, max)
end
end
Note that, with this script, min and max have to be integers and max has to be greater than min, else Random:NextInteger will error. Changing this could be an exercise for you if you'd like to practice.
8 comments
what do you mean by random sequence? mind giving some examples?
So I am wanting the for loop to go up randomly, instead of going up by the same amount each time.
I still don't understand
That isn't exactly how for loops work. You can't change the iteration count afaik. What are you planning to use this for and maybe we can give you a better alternative than what you're planning.
It's probably possible to do with a while loop instead of a for loop.
is List a table or an integer? and if im getting what youre trying to do, could you possibly be trying to make a loading bar that randomly goes up? if yes then what Rheines said should work; you should go for a while loop
I've edited by answer for you
my*