Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Creating Biased Random With Given Code?

Asked by 5 years ago

I have a game that randomly selects 2 people. The thing that's making players mad is that some people go more than others. How would I go on implementing a script to make it so that the last 2 people that have played don't go again for the next couple of games?

local Players = game.Players:GetPlayers() 
math.randomseed(tick()) 
local Chosen1, Chosen2 = math.random(1, #Players) 
repeat wait() 
    math.randomseed(tick()) 
    Chosen2 = math.random(1, #Players) 
until Chosen2 ~= Chosen1 
local P1 = Players[Chosen1] 
local P2 = Players[Chosen2] 
print(P1.Name) 
print(P2.Name)
1
Insert the last two players into a table and check that the chosen players are not in the table , and them remove them from the table when you want them to be able to play again. awfulszn 394 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

Try this:

math.randomseed(tick())
local Players = game:GetService('Players')

local Chosen = {}

local function ChooseRandomPlayers()
    local R1, R2 = math.random(1, #Players:GetChildren())
    return R1, R2
end)

repeat wait()
local R1, R2 = ChooseRandomPlayers()
until not Chosen[Players[R1]] and not Chosen[Players[R2]] do end
Chosen[Players[R1]] = true
Chosen[Players[R2]] = true

This is a really rough code. But it has the basic table elements down. What you can do is reset all the players to false after everyone has gone once. to reset it. Hopefully this helped. Another thing you can do is store the number of times the player has been chosen and then compare for the smallest values.

Ad

Answer this question