Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Random Generator Picks the Indy 500 Winner

DZone's Guide to

Random Generator Picks the Indy 500 Winner

Let's employs a previously covered tool—Random Generator—to pick the winner of the upcoming Indy 500 race, displaying probability and randomness in Java in action.

· Java Zone
Free Resource

Never build auth again! The Okta Developer Platform makes it simple to implement authentication, authorization, MFA and more in Java applications. Get started with the free API.

With the qualifications completed and the field set for the 101st running of the "Greatest Spectacle in Racing," I thought it would be cool to see which driver would be picked by Random Generator to get the win for the 2017 Indy 500.

Image titlePhoto courtesy of Bryon J. Realey

For those who are not aware of Random Generator, you can read my series of articles on DZone:

Or, you can review the source code on GitLab.

Setting Up the Data

Since Random Generator can process a  java.util.List object, I created a simple Participant object to house each car that qualified for the Indy 500.

import lombok.Data;

@Data
public class Participant {
    private Integer startPosition;
    private Integer carNumber;
    private String driverName;
    private String odds;
    private Integer rating;
}


If you are not aware of Lombok, it is a great utility to keep from having to manually create getters, setters, and a few other boilerplate elements in POJOs.

Giving Odds to The Drivers

Like any race with established participants, odds are generated for those placing wagers on the competition. While I am not a gambling person, I did decide to use these odds to weigh the participants for the Indy 500 race. After all, the drivers in the race certainly have different skill sets and experience — which has proven to be helpful for the first 100 races held in Speedway, Indiana.

According to SportsBook.ag, the drivers with the best odds are Helio Castroneves, Juan Pablo Montoya, Scott Dixon, and Tony Kanaan with 8:1 odds. The remainder of the field has odds that vary from 9:1 to unlisted. For those unlisted, I gave them odds of 99:1 to win the race.

In order to translate the odds into a "rating" field that Random Generator can understand, I used the following logic:

 100 - the antecedent (or left hand portion of the ratio) 

So, those with 8:1 odds were given a rating of 92 (100 - 8).

When enabling the rating functionality in Random Generator, the rating field will be used to favor drivers with better odds of winning the race. For this experiment, I opted to use a rating level of 2 (RATING_LEVEL_MEDIUM).

Random Generator in Action

With the List<Participant> set, I was able to run Random Generator using the following line of code:

 List results = randomGenerator.randomize(participants, 33, 2);  

From there, I can use a simple System.out  command to paint the order Random Generator believes will occur:

System.out.println("\nRandom Generator Results:");
for (int i = 0; i < results.size(); i++) {
    System.out.println((i + 1) + ". " + results.get(i).getDriverName() + " ("
            + results.get(i).getCarNumber() + ") {Odds = " + results.get(i).getOdds() + "}");
}


My results provided the following finishing order for the 101st running of the Indy 500 on May 28th, 2017:

Random Generator Results:
1. Tony Kanaan (10) {Odds = 8/1}
2. Josef Newgarden (2) {Odds = 9/1}
3. Will Power (12) {Odds = 10/1}
4. Graham Rahal (15) {Odds = 25/1}
5. James Hinchcliffe (5) {Odds = 15/1}
6. Jack Harvey (50) {Odds = 50/1}
7. Juan Montoya (22) {Odds = 8/1}
8. Helio Castroneves (3) {Odds = 8/1}
9. Sebastien Bourdais (18) {Odds = 60/1}
10. Alexander Rossi (98) {Odds = 20/1}
11. Ryan Hunter-Reay (28) {Odds = 10/1}
12. Marco Andretti (27) {Odds = 10/1}
13. Carlos Munoz (14) {Odds = 18/1}
14. Simon Pagenaud (1) {Odds = 10/1}
15. Ed Carpenter (20) {Odds = 25/1}
16. Takuma Sato (26) {Odds = 25/1}
17. Scott Dixon (9) {Odds = 8/1}
18. Oriol Servia (16) {Odds = 80/1}
19. Charlie Kimball (83) {Odds = 25/1}
20. Fernando Alonso (29) {Odds = 15/1}
21. JR Hildebrand (9) {Odds = 20/1}
22. Jay Howard (77) {Odds = 99/1}
23. Mikhail Aleshin (7) {Odds = 80/1}
24. Max Chilton (8) {Odds = 60/1}
25. Pippa Mann (63) {Odds = 99/1}
26. Spencer Pigot (11) {Odds = 99/1}
27. Buddy Lazier (44) {Odds = 99/1}
28. Zach Veach (40) {Odds = 99/1}
29. Connor Daly (4) {Odds = 99/1}
30. Gabby Chaves (88) {Odds = 99/1}
31. Sebastian Saavedra (17) {Odds = 99/1}
32. Sage Karam (24) {Odds = 99/1}
33. Ed Jones (19) {Odds = 99/1}


Have a really great day!

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:
java ,random ,lombok ,probability ,tutorial

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}