Random:NextInteger() help?

Hey guys!

So recently, I’ve been thinking of creating a game based on Agatha Christie’s “And Then There Were None” Poem/screen write, so I’ve been messing around with some code in order to choose a random player to become the murderer (The “Riddler” in my case to keep it roblox friendly) however I’ve reached a point where I have no idea why it is erroring.

 _G.PlayerRiddler = nil


for i,v in pairs(game.Players:GetPlayers()) do
	if i == Random:NextInteger(1,#game.Players:GetPlayers()) then
		_G.PlayerRiddler = v.Name
	end
end

Is the code I have currently, yet I get an error stating

[ServerScriptService.Core:5: attempt to call method 'NextInteger' (a nil value)]

Do take in mind that I have heard that math.random() is unreliable therefore I have been using Random:NextInteger()

I am frankly still a noob at coding so I will need to do a lot of research to finish this game, but this is just the start, so if I could possibly get some pointers on how to either fix this or a better way on selecting a person (I want to avoid bool values, since they are quite exploitable) that would be amazing! Thanks !

~Revelted

1 Like
 _G.PlayerRiddler = nil


for i,v in pairs(game.Players:GetPlayers()) do
	if i == Random.new():NextInteger(1,#game.Players:GetPlayers()) then
		_G.PlayerRiddler = v.Name
	end
end
6 Likes

face palm

Sorry about that i just realized that >.<

You need to first create new Random object by doing x = Random.new(seed) and then calling the
function NextInteger on it.

--// Example
local x = Random.new();
print(x:NextInteger(0, 100))

Oops, didn’t see post above.

1 Like

all good! Thanks again you guys lol

Again, math.random isn’t unreliable. It’s much faster and uses the exact same API.
image

I don’t know where you heard that, but I’d just disregard it. It’s false information.

3 Likes

The speed difference between math.random and the Random object is negligible, so this isn’t a point of interest to remark on.

The main difference is the capabilities of each. math.random is a standalone function in the math library, while the Random object sports a selection of API you can use, up to branching results and keeping a seed relative to an object rather than globally, which is what math.randomseed(x) implies.

math.random isn’t unreliable, you’re right there. math.random and Random’s algorithms are the same, you’re right there. Random is typically regarded as much more powerful though and speed is completely irrelevant. That’s heading towards microoptimisation grounds.

2 Likes

Please don’t do this. You’re regenerating the random number for each player, so it’s possible that nobody is marked as the killer. Instead, generate the number outside the loop and then compare it to i.

In fact, you don’t even need a loop here. You can just take a random player from a table.
For example:

local rand = Random.new()
local players = game:GetService("Players"):GetPlayers()
local selectedPlayer = players[rand:NextInteger(1, #players)]

_G.PlayerRiddler = selectedPlayer.Name
2 Likes

Random library does have NextNumber, which is much easier and nicer looking than the math.random alternative. The API is also much nicer to look at, even if you can’t do NextInteger(#Whatever).

1 Like

What do you mean? Yes you can do this. NextInteger supports a minimum and maximum value.

I meant how you can do math.random(5), but for Random you have to do ::NextInteger(1, 5).

1 Like

Actually I conducted a test and found these results:


As you can see I tested it up to 5000 and found little difference between.
In conclusion neither is better.
(Sorry for the bad test grid [paint.net lol] )

2 Likes

He stated that their algorithms are the same, so your test only proved what he was saying, not to the contrary. He only states that Random provides an object with which can have an individual seed, rather than math.random’s global seed along with other advantages.

I’ve said it before and I’ll say it again:

  • Use Random if you need a discrete state and a nicer API.
  • Use math.random if you don’t care about either of those things.
  • Speed difference is marginal and not worth the attention you’re giving it
2 Likes

So that’s how you check if Random.ew():NextInteger() is on a specific value… Interesting.