Okay, so I probably just don’t understand how it works, but I’m having an issue with randomseed. I have a folder of 100 numbered instances and I’m picking a random one and deleting it, restarting the script, then picking another. I expect it to choose randomly every time, but it seems to just count down after the first, although when it reaches some numbers it seems to count down from the first number it picks, then continue counting down. Then at the end it counts up??? I’m not sure why.
Here’s the code:
wait(.1)
math.randomseed(tick())
local values = script.Parent.Folder:GetChildren()
local randomchoice = values[math.random(1,#values)]
print(randomchoice.Name)
randomchoice:Destroy()
wait()
local s = script:Clone()
s.Parent = workspace
script:Destroy()
And here’s the sequence it prints the numbers. Only the first one seems to be random:
Using :NextInteger means the result will always be a whole number. Using :NextNumber means that the result can be a decimal, negative, whole number, etc.
A typical property of random number generators is that re-seeding – especially with well-spaced numbers – will give predictable output.
Don’t clone & reseed between each call. In fact, if you get rid of the randomseed, you’ll fix your problem. I don’t see any good reason why it’s in there. Usually, you use randomseed once at the beginning, and never touch it again (unless you’re looking to achieve repeatable output, but in that case, the “Random” class is probably what you want).
math.randomseed sets the seed globally (as in, for the whole identity), and if you are cloning a script which also uses math.randomseed you are setting it for all scripts - that’s probably why you’re getting that pattern.
The old math.randomseed is pretty useless in Studio too, since it there are things beside your code reseeding it and drawing values from the RNG, possibly even between consecutive lines of your Lua, even when there isn’t any yielding. Plus both your client and server using the same global RNG when you test locally, which of course does not happen with your published game. So yeah, that’s why there is the new Random class
You really don’t need to do that anymore. Either do what Oryxide said or just leave it as math.random (I’m pretty sure they use the same algorithm after the most recent update).
Uhh… hello?? Is nobody going to give an actual answer as to specifically why the code the OP posted isn’t working?
The problem with the code you posted is that while the value of tick() changes on every run of the script, math.randomseed() only uses the integer part of the number that you pass to it. So effectively you were re-using the same seed 10 times in a row (since your script runs 10 times per second). Thus you see exactly consecutive numbers being chosen about 10 times in a row. (Given that random presumably uses modular arithmetic internally, with the same seed but a slightly different range because one of the items was removed you would expect to get an answer 1 different)
If you simply change your code to math.randomseed(tick() * 1000) it will fix the issue by making randomseed get a different input every time. There’s absolutely no reason to use Random.new() here, that’s just overkill—math.random / randomseed are perfectly fine for this task.
I noticed immediately after posting that running it 10 times in a second could’ve been what was causing it and tried running it once a second instead, result seems the same. Multiplying tick() by 1000 also doesn’t seem change the result. I tried not cloning the script as was recommended by many developers who I’ve probably given migraines and it seems to have worked after some modifications (I should’ve made it more clear that the example I posted isn’t exactly what my script is but a reproduction to show my problem, for loops would not have worked). Thanks for everyone for the help!
WTF, I’m not sure what’s going on anymore, anyone have any ideas:
a = math.floor((tick()%1)*1000);
math.randomseed(a)
print(math.random(1,76))
wait()
b = math.floor((tick()%1)*1000);
math.randomseed(b) print(math.random(1,75))
print(a, b)
When you paste the following code into the command line in studio you find that the two generated numbers are the same like… 80% of the time, and when they aren’t they differ by exactly 1 (Basically what the OP was seeing—looks like I spoke to soon on what the issue actually was). This even though the randomseed inputs are substantially different. How does that behavior arise? I’m not sure what RNG implementation could actually cause that behavior.
No clue, but in the newest release there’s a pending change that changes the algorithm of math.random. Maybe try this block of code again once that’s out?
If you’re dying to know the only surefire way of knowing would be reverse engineering, which I would advise against…or just ask one of the people who work on the client. I am curious too, though.