Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I create a randomised for loop?

Asked by 5 years ago
Edited 5 years ago

How would I make a random for loop? This is what I am trying to achieve:

for 1,10 do math.random
end

Though obviously math.random doesn't work due to the fact it isn't a number. So how would I achieve a random sequence?

This is the section of my script that contains the for loop I am talking about

for i = 0, List, 1 do
    LoadingBar.Size = UDim2.new(i/List, 0, 0.008, 0)
     Percentage.Text = tostring(math.floor(tonumber (i/List)*100)) .. "%"
     wait()
    if i == List then 
    wait(0.5)
    BackgroundFrame:TweenSize(UDim2.new(1, 0, 0, 0), Enum.EasingDirection.In,Enum.EasingStyle.Quad, 1)
    wait(1)
    BackgroundFrame:Destroy()

  end
 end
end)
0
what do you mean by random sequence? mind giving some examples? GoldAngelInDisguise 297 — 5y
0
So I am wanting the for loop to go up randomly, instead of going up by the same amount each time. ConnorThomp 87 — 5y
0
I still don't understand Creacoz 210 — 5y
0
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. SummerEquinox 643 — 5y
View all comments (4 more)
0
It's probably possible to do with a while loop instead of a for loop. Rheines 661 — 5y
0
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 GoldAngelInDisguise 297 — 5y
0
I've edited by answer for you BenSBk 781 — 5y
0
my* BenSBk 781 — 5y

1 answer

Log in to vote
0
Answered by
BenSBk 781 Moderation Voter
5 years ago
Edited 5 years ago

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:

-- Create a new Random object.
local random = Random.new()

-- Output i for each value of i from 1 to a random number between 1 and 100.
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()

-- Use a do block to limit the scope of used variables.
do
    local start_n = 1 -- Start number.
    local end_n = 100 -- End number.
    local min, max = 1, 20 -- Minimum and maximum random number.
    local i = start_n
    -- Execute body and increment until i > end_n.
    while i <= end_n do
        -- ...
        i = i + random:NextInteger(min, max) -- Increment i.
    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.

0
You need another condition in the while loop because the way it is set up currently allows for i to be greater than end_n. Rheines 661 — 5y
0
No, it doesn't. `while i <= end_n do` BenSBk 781 — 5y
0
OP requires the i variable and currently i can go above 100. Rheines 661 — 5y
0
The i variable is assigned to `start_n` and incremented appropriately. You cannot go above 100 with my code. BenSBk 781 — 5y
View all comments (10 more)
0
This happens when I print(i) : 7 14 16 25 37 45 53 69 86 105 Rheines 661 — 5y
0
If you change NextInteger to just run on 1,100-i ; and end_n to 99 you can probably get closer to the result you are hoping to achieve. https://hastebin.com/xovonikazi.bash SummerEquinox 643 — 5y
0
That's probably because you're printing i below the increment. Your code should be above, as demonstrated by the comment. BenSBk 781 — 5y
0
If you are printing above then we are below 100, not at 100. OP's percentage won't display 100. Rheines 661 — 5y
0
I don't understand what you're talking about. The `-- ...` demonstrates where your code should go. BenSBk 781 — 5y
0
OP doesn't necessarily need 100 to ever be assigned to i. They want random a increment. BenSBk 781 — 5y
0
After rereading the question I think you're right. my bad Rheines 661 — 5y
0
@SummerEquinox, that's not the intended behaviour, but thank you BenSBk 781 — 5y
0
The initial question is really confusing me in how it is worded :( SummerEquinox 643 — 5y
0
Ya, they just want a random increment per iteration of a numeric for statement BenSBk 781 — 5y
Ad

Answer this question