Skip to content

Vote Randomizer

Reference

class VoteRandomizerVoteRandomizer->getNewVote()

The VotesRandomGenerator class allows to generate random votes respecting some probability criteria.
The randomness can be cryptographically secured (default) or follow a reproducible sequence thanks to the use of a seed or the PHP Randomizer of your choice.

The class is not linked to a living election. Candidates can be Candidate object or string. It's return new vote object containing untouched candidates inputs. You can also provide an array a a candidate to forcing ties between differents candidates, each time.

php
use CondorcetPHP\Condorcet\Candidate;
use CondorcetPHP\Condorcet\Tools\Randomizers\VoteRandomizer;

for ($i = 1; $i <= 3; $i++) {
    $candidates[$i] = new Candidate("Candidate $i");
}

$voteRandomizer = new VoteRandomizer($candidates);

$newVote1 = $voteRandomizer->getNewVote();
$newVote2 = $voteRandomizer->getNewVote();
$newVote3 = $voteRandomizer->getNewVote();

Options can be passed as object variable at any time:

php
use CondorcetPHP\Condorcet\Candidate;
use CondorcetPHP\Condorcet\Tools\Randomizers\VoteRandomizer;

for ($i = 1; $i <= 6; $i++) {
    $candidates[] = new Candidate("Candidate $i");
}

$voteRandomizer = new VoteRandomizer($candidates);

// If not null, each vote will rank maximum of 4 candidates but a minimum of 3.
$voteRandomizer->maxCandidatesRanked = 4;
$voteRandomizer->minCandidatesRanked = 3;
$newVote1 = $voteRandomizer->getNewVote();

// Each vote will rank 4 candidates
$voteRandomizer->maxCandidatesRanked = 4;
$voteRandomizer->minCandidatesRanked = true;
$newVote2 = $voteRandomizer->getNewVote();

// Not a question of candidates, but max number of ranks
$voteRandomizer->maxRanksCount = 3;
$newVote3 = $voteRandomizer->getNewVote();

// Each vote has 50.42% of probability to get a least 1 tie in a rank.
$voteRandomizer->tiesProbability = 50.42;
$newVote4 = $voteRandomizer->getNewVote();

// Each vote will have at least 2 ties, and 50% chance of at least one more.
$voteRandomizer->tiesProbability = 250;
$newVote5 = $voteRandomizer->getNewVote();

Candidates can be changed (and seed preserved) at any time:
<<< @/code_snippets/vote_randomizer_change_candidates.php

Released under the MIT License.