Math.randomseed seemingly broken?

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:

Value13
Value100
Value99
Value98
Value97
Value96
Value12
Value94
Value93
Value92
Value91
Value90
Value89
Value88
Value11
Value86
Value85
Value84
Value83
Value82
Value81
Value80
Value10
Value78
Value77
Value76
Value75
Value74
Value73
Value72
Value9
Value70
Value69
Value68
Value67
Value66
Value65
Value64
Value8
Value62
Value61
Value60
Value59
Value58
Value57
Value56
Value7
Value54
Value53
Value52
Value51
Value50
Value49
Value48
Value6
Value46
Value45
Value44
Value43
Value42
Value41
Value5
Value39
Value38
Value37
Value36
Value35
Value34
Value33
Value4
Value31
Value30
Value29
Value28
Value27
Value26
Value25
Value3
Value23
Value22
Value21
Value24
Value32
Value40
Value47
Value2
Value55
Value63
Value71
Value79
Value87
Value95
Value14
Value1
Value15
Value16
Value17
Value18
Value19
Value20

Does anyone know a way around this issue? I’m stumped.

1 Like

I would recommend using the new random function.

local rng = Random.new()

Then you can use

rng:NextInteger(minvalue, maxvalue)

or

rng:NextNumber(minvalue, maxvalue)

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.

8 Likes

Here’s a link to the dev forum post about it with more information.

1 Like

Just wondering, but why are you cloning the script in order to re-execute it? Do you know how to use a for-loop?

1 Like

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).

2 Likes

Don’t Clone a script to reexecute it, that’s not only weird but it’s also probably what’s wrong.

Use the new Random class and use a function instead of cloning another script, assuming that wasn’t just for show.

1 Like

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.

You should be using the new Random class instead.

1 Like

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 :slight_smile:

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).

cough for loops please mr bucket

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.

11 Likes

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!

2 Likes

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.

3 Likes

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?

Nooo stop the release! I have to figure out what’s going on first, otherwise it will forever remain a mystery!

3 Likes

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.