0% found this document useful (0 votes)
5 views4 pages

Random Voting Simulation in Java & Python

Uploaded by

nagpalsanjna1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Random Voting Simulation in Java & Python

Uploaded by

nagpalsanjna1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java

// Input choice: votes are randomly generated using [Link].


// Output: a single line with only the winner's name.
// Notes for quick tests are included as commented blocks at the bottom.

import [Link];

public class Main {


public static void main(String[] args) {
String[] candidates = {"Alice", "Ben", "Clara", "David", "Eva"};
int nCandidates = [Link];
int[] counts = new int[nCandidates];
Random rng = new Random(); // optionally: new Random(42) for repeatable tests

int winnerIndex = -1;

// Process up to 200 votes, stop early if someone exceeds 100


for (int i = 0; i < 200; i++) {
int vote = [Link](nCandidates); // 0..4
counts[vote]++;

if (counts[vote] > 100) {


winnerIndex = vote; // early stop winner
break;
}
}

// If no early winner, find highest total and resolve ties randomly


if (winnerIndex == -1) {
// Find max
int maxCount = -1;
for (int i = 0; i < nCandidates; i++) {
if (counts[i] > maxCount) {
maxCount = counts[i];
}
}
// Collect indices tied at max
int[] tied = new int[nCandidates];
int tiedSize = 0;
for (int i = 0; i < nCandidates; i++) {
if (counts[i] == maxCount) {
tied[tiedSize++] = i;
}
}
// Pick at random among tied candidates
if (tiedSize == 1) {
winnerIndex = tied[0];
} else {
int pick = [Link](tiedSize);
winnerIndex = tied[pick];
}
}

// Print only the winner's name


[Link](candidates[winnerIndex]);
}
}

/*
Suggested quick tests (uncomment to use, then recomment for normal random run):

1) Force early win:


- Replace the vote line with:
int vote = 0; // Alice gets all votes -> exceeds 100 quickly

2) Force final-tally tie between Alice (0) and Ben (1):


- Before the for-loop, set:
for (int i = 0; i < 100; i++) { counts[0]++; counts[1]++; }
int iStart = 200; // skip loop body by setting start beyond 200
- Or inside the loop, alternate votes deterministically:
int vote = (i % 2 == 0) ? 0 : 1; // ends 100-100 tie after 200 votes
*/

Python

# Input choice: votes are randomly generated using the standard random module.
# Output: a single line with only the winner's name.
# Quick tests are shown at the bottom as comments.

import random

candidates = ["Alice", "Ben", "Clara", "David", "Eva"]


counts = [0, 0, 0, 0, 0]
# [Link](42) # optional: enable for reproducible runs

winner_index = -1

# Process up to 200 votes, stop early if someone exceeds 100


for _ in range(200):
vote = [Link](0, 4) # 0..4
counts[vote] += 1
if counts[vote] > 100:
winner_index = vote
break

# If no early winner, find highest total and resolve ties randomly


if winner_index == -1:
# Find max "manually" without Counter
max_count = -1
for c in counts:
if c > max_count:
max_count = c
# Collect indices tied at max
tied = []
for i, c in enumerate(counts):
if c == max_count:
[Link](i)
# Choose from tied list at random
if len(tied) == 1:
winner_index = tied[0]
else:
winner_index = tied[[Link](0, len(tied) - 1)]

# Print only the winner's name


print(candidates[winner_index])

# --- Suggested quick tests (uncomment to try) ---


# 1) Force early win:
# for i in range(200):
# vote = 0 # Alice
# counts[vote] += 1
# if counts[vote] > 100:
# winner_index = vote
# break
# print(candidates[winner_index])

# 2) Force exact tie between Alice and Ben after 200 votes:
# counts = [100, 100, 0, 0, 0]
# winner_index = -1
# # Tie-break path runs:
# max_count = 100
# tied = [0, 1]
# winner_index = tied[[Link](0, len(tied) - 1)]
# print(candidates[winner_index])
Handling early stopping, ties, randomness

Early stopping: After each vote is recorded, the program checks if that candidate’s count is
> 100. If true, it immediately stops processing further votes and declares that candidate the
winner.

Final tally: If no one passes 100 by the time up to 200 votes are processed, it scans for the
maximum count.

Tie breaker: If multiple candidates share the maximum, it selects the winner uniformly at
random among the tied candidates (using [Link] in Java and [Link] in
Python).

You might also like