Problem with math.random not giving random number

Hi, my name is shieldmr3, my main thing/job is scripting, I do some building too. I’m an expert at lighting and scripting them!

I have been using math.random a lot, I have noticed that when I make a random number using math.random, it keeps generating the same number every time, here’s an example:

local randomnumber = math.random(1, 50)

while wait() do
        print(randomnumber)
end

So when it prints, it keeps printing the same number every time.

how can this be fixed? is this a problem with my script? and if it is, then how can I fix it?

2 Likes

that is cause the variable doesnt change, if you want it to, move the variable to the while loop

while wait() do
    local randomnumber = math.random(1, 50)
    print(randomnumber)
end
6 Likes

If you store it in a variable, randomnumber will be the same number. So do

while wait() do
        print(math.random(1, 50))
end
4 Likes

Oh I get it, thank you so much!

1 Like

Thank you too! I didn’t really understand why, but now I get it. Thank you all!

2 Likes

I know everyone gave the answer, but I’ll provide anotehr way to do it

while wait() do
    local randomnumber = math.random(50)
    print(randomnumber)
end

If your minimum is 1, if you just specify a single number, it’ll pick from 1 to that specified number, in this case, a number between 1 and 50

2 Likes

Thank you, I appreciate everyone trying to help! Thank you all!

1 Like

Anytime! If you have anymore questions or issues don’t be afraid to make another post!

1 Like

I will! Thank you so much! I know my question sounded kind of a beginner but, if you need any help, just ask me, I make lots of cool things such as an animated jumpscares, here is the link if you want to see it:

I make most of the things, but I like unique things such as jumpscares, camerashake, guis (announcements gui, ban and kick, suggestion that sends it to your discord, main game screen (kind of an intro, animating, etc.)

If you or anyone needs help, please ask me, I like helping people as much as possible!

1 Like

put variable in loop bc the math. Random only gets run once putting it any type of loop will work

1 Like

I did, thank you for your help! I appreciate it!

1 Like

Be sure to give FactorOfTheThird the solution mark for giving you the correct solution

Hi, well, they all gave me an answer, and I appreciate that. But they all answered at the same time, even tho FactorOfTheThird was first, but all of them answered right after. So I didn’t give anyone solution just to be fair. I did give them all likes too.

Oh it’s not a matter of being fair it’s just so people can see that it’s been solved

2 Likes

Ok, I’ll do that now, thank you.