SoFunction
Updated on 2025-03-02

Three ways to implement random lottery in Java

introduction

Implementing random lottery methods in Java, usually we useClasses to generate random numbers, and then select winners based on these random numbers. The following will give several common random lottery implementation methods, including drawing from arrays, drawing from lists, and weight-based lottery methods.

1. Extract from the array

import ;  
  
public class LotteryFromArray {  
    public static void main(String[] args) {  
        String[] candidates = {"Alice", "Bob", "Charlie", "David", "Eva"};  
        Random random = new Random();  
          
        // Generate a random number between 0 and -1        int index = ();  
          
        // Output the winner        ("The winner is:" + candidates[index]);  
    }  
}

2. Extract from the list

useArrayListorLinkedListCollections such as this can also implement lottery draws, especially when dynamically adding or deleting candidates are required.

import ;  
import ;  
import ;  
  
public class LotteryFromList {  
    public static void main(String[] args) {  
        List<String> candidates = new ArrayList<>();  
        ("Alice");  
        ("Bob");  
        ("Charlie");  
        ("David");  
        ("Eva");  
          
        Random random = new Random();  
          
        // Generate a random number between 0 and ()-1        int index = (());  
          
        // Output the winner        ("The winner is:" + (index));  
    }  
}

3. Weight-based lottery

In some cases, each candidate's chance of winning may be different, which requires the implementation of a weight-based lottery.

import ;  
import ;  
import ;  
  
public class LotteryWithWeights {  
  
    static class Candidate {  
        String name;  
        int weight; // Weight  
        public Candidate(String name, int weight) {  
             = name;  
             = weight;  
        }  
    }  
  
    public static void main(String[] args) {  
        List<Candidate> candidates = new ArrayList<>();  
        (new Candidate("Alice", 1));  
        (new Candidate("Bob", 3));  
        (new Candidate("Charlie", 1));  
        (new Candidate("David", 2));  
        (new Candidate("Eva", 3));  
  
        Random random = new Random();  
        int totalWeight = 0;  
        for (Candidate candidate : candidates) {  
            totalWeight += ;  
        }  
  
        int target = (totalWeight) + 1;  
        int sum = 0;  
        for (Candidate candidate : candidates) {  
            sum += ;  
            if (sum >= target) {  
                ("The winner is:" + );  
                break;  
            }  
        }  
    }  
}

In the weight-based draw example above, we define a Candidate class to store the candidate's name and weight. The winner is then determined by accumulating the weights and generating a random number. Note that here we use (totalWeight) + 1 to ensure that the generated random numbers are from 1 to the total weight (included), thereby avoiding the problem caused by 0 values. Finally, by traversing the candidate list and accumulating the weight, the first candidate greater than or equal to the random number is found as the winner.

The above three methods are applicable to different scenarios and can be selected and used according to actual needs.

This is the end of this article about the summary of Java's methods to implement random lottery. For more related Java random lottery content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!