Random.new(nil) throws error while Random.new() does not

Reproduction Steps

Call Random.new(nil) with a nil argument given.

local rand = Random.new(nil)
local randomSeed = nil
local rand = Random.new(randomSeed)

repo.rbxl (40.0 KB)

Expected Behavior

Act as all other objects behave, where an argument nil is treated the same as no argument at all, and create a default object. As per the documentation, this should pull a random seed from elsewhere.

Actual Behavior

Throws the following error:

invalid argument #1 to 'new' (number expected, got nil) 

Workaround

Use a ternary operator if randomSeed is nil

local function getRandom(randomSeed: number?)
    return randomSeed and Random.new(randomSeed) or Random.new()
end

Issue Area: Engine
Issue Type: Other
Impact: Low
Frequency: Constantly
Date First Experienced: 2023-01-17 11:01:00 (-05:00)

6 Likes

Thanks for the report! We’ve filed a ticket in our internal database.

1 Like

There is no such requirement on Roblox APIs to treat missing arguments and ‘nil’ as the same thing.
They behave differently in many contexts.

Documentation states “If you don’t provide the seed parameter”. Passing ‘nil’ is providing the argument, so the default doesn’t apply.

We will consider this report as a feature request.

1 Like

Done. Random.new(nil) is now equivalent to Random.new().

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.